Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert time (hh:mm) to decimal form with momentjs

Tags:

momentjs

what is the best way to convert Hours Minutes to Decimal Time with momentjs

let say i have 1:30 min and i want to have 1.5 after the conversion

here is what i have tried

console.log(travelTime().split(':')[0]);   return 1
console.log(travelTime().split(':')[1]);   return 30  then / 30

where travelTime() == "1:30"

like image 860
Gildas.Tambo Avatar asked Jan 21 '14 10:01

Gildas.Tambo


People also ask

What is _D and _I in moment?

_i can also be undefined, in the case of creating the current moment with moment() . _d is the instance of the Date object that backs the moment object. If you are in "local mode", then _d will have the same local date and time as the moment object exhibits with the public API.

How do you get time in 24 hour format?

Try: moment({ // Options here }). format('HHmm') . That should give you the time in a 24 hour format.

Is MomentJS deprecated?

MomentJs recently announced that the library is now deprecated. This is a big deal for the javascript community who actively downloads moment almost 15 million times a week.

How do you convert time into moments?

Convert time If you want to create a moment object using current date and time, just call moment() without any arguments.


1 Answers

It looks like what you want is moment.duration(durationAsString) to parse your duration and .asHours() to format as decimal.

moment.duration('1:30').asHours(); will return 1.5.

For more information, see Moment's documentation section on durations: http://momentjs.com/docs/#/durations/

like image 164
pauljz Avatar answered Sep 22 '22 06:09

pauljz