Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generating Duration from Time Values

I am trying to find a way via moment.js to convert two time values, which are coming in as as start and stop times, expressed in hours and minutes as string values, like this: start: 1:12, and stop: 2:10, or in the afternoon, start: 14:23, and stop: 15:22 and generate a duration in milliseconds from this. I have tried the following, but it gives me a NaN result:

Just to clarify, I am receiving the data in string format. See below for what that looks like:

const startValue = "1:12";
const stopValue = "2:10"; // for a duration of 58 minutes

const msDuration = moment(stopValue).format('HH:mm').valueOf() - moment(startValue).format('HH:mm').valueOf();

I assume the issue here is that my startValue and stopValue need to be converted to a different format first, since they are currently string values? So, how can I convert a string value of "1:12" to be something moment can use?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Based on your comments above, I recognize that you are receiving the times as a response in the following format: "HH:mm".

If the actual Date doesn’t really matter, then you can convert it like this (JsFiddle):

const startValue = "1:12";
const stopValue = "2:10"; // for a duration of 58 minutes
const start = { hour: startValue.split(":")[0], minute: startValue.split(":")[0] }
const stop = { hour: stopValue .split(":")[0], minute: stopValue .split(":")[0] }

const msDuration = moment().hours(stop.hour).minutes(stop.minute).valueOf() - moment().hours(start.hour).minutes(start.minute).valueOf(); //you might get 1 ms less. so jest set also ms to 0
const msDurationA = moment().hours(stop.hour).minutes(stop.minute).milliseconds(0).valueOf() - moment().hours(start.hour).minutes(start.minute).milliseconds(0).valueOf();

So basically you are taking the current date and mutate the hours and minutes, so you can work with moment.Js.

Although my question is, why do you necessary need moment? you can also do it in native JS:

const startValue = "1:12";
const stopValue = "2:10"; // for a duration of 58 minutes

const start = { hour: +startValue.split(":")[0], minute: +startValue.split(":")[1] };
const stop = { hour: +stopValue.split(":")[0], minute: +stopValue.split(":")[1] };

const msDuration = (stop.hour * 3600000 + stop.minute * 60000) - (start.hour * 3600000 + start.minute * 60000);

console.log("msDuartion: ", msDuration);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading