Moment.JS - Change a two digit value to milliseconds using moment


At the moment I have an input field that allows you to choose an hour by entering two digits. for example you could choose 12 for 12 hours.

Is there a way using moment to convert that number into milliseconds using MomentJS?

At the moment I am having to do the below maths. I can't see in the Moment docs that this is doable.

var timeHH = scope.session.timeHH * 3600;

This works out the seconds, then later I mulitply it by 1000 for the milliseconds value.



Is there a way using moment to convert that number into <s>mini</s> milliseconds using moment?


There may be, but there's absolutely no reason to use MomentJS for this, and doing so would be roundabout and inefficient. It's quite straightforward: hours * 3600000 is milliseconds. There are no weird special cases to handle, etc., unless you're handling converting a particular period of hours of a real time (say, the 10 hours from 8 p.m. December 31st 2005 GMT) and want to handle leap second insertions (there was one that night at midnight), but MomentJS doesn't do that anyway.


For this particular use case, the right thing to do is a manual calculation. For the sake of completeness and in the event you needed to do more complicated calculations, to do this with Moment, you would use the duration type. The duration type will allow you to convert from one unit value to another, and if you had to make several unit conversions instead of just this one it would be a good choice.

moment.duration(12, 'hours').asMilliseconds() 43200000

In addition to converting to milliseconds, you could convert to any other unit:

var dur = moment.duration(12, 'hours'); dur.asMilliseconds(); 43200000 dur.asDays(); 0.5 dur.asYears(); 0.001368953503494254

In addition to the as functions, you can get the parts of the duration broken out:

var dur = moment.duration(12.5, 'hours'); dur.hours(); 12 dur.minutes(); 30 dur.seconds(); 0

Or call humanize to get a human readable string that is an estimate of the duration's length:

var dur = moment.duration(42, 'hours'); dur.humanize(); "2 days"

You can also do math with durations. See the docs for more info: <a href="http://momentjs.com/docs/#/durations/" rel="nofollow">http://momentjs.com/docs/#/durations/</a>


You can try the following hack(assuming hours in 24-Hours format) :

var momObj = moment('12', 'HH'); var x = momObj.diff(moment().startOf('day')); console.log(x) //43200000


  • avconv save 10 frames and update them in cycle
  • Regex to match exactly n occurrences of letters and m occurrences of digits
  • Use case for private changes
  • iconv returns strange results
  • Get result sql from Prepared Statement by Oracle
  • Download data first then show it on TableViewcontroller
  • C#'s “protected internal” means “protected” *OR* “internal”. Does any keyword mean “protected”
  • 1º Day of Daylight Saving Time Java and JS showing a different behavior
  • Handling exceptions in a class library enveloping a device driver
  • Is MVC2 ASP.Net URLDecoding automatically?
  • Shadows for Instantiated Game Object bug?
  • Two columns in subquery in where clause
  • UML diagram generator in Visual Studio 2010
  • CKeditor stripping font tags instead of converting to span
  • Graceful pod termination
  • Basic defensive programming [duplicate]
  • Creating a C++ function that calls other Lua function
  • Draw half infinite lines?
  • back button function for phonegap windows phone 7
  • Why isn't my “Fizz Buzz” test in R working?
  • crash in __tcf_0
  • Replace last two characters in column
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • Validate jQuery plugin, field not required
  • For loop with if condition on multiple R functions
  • Trying to get the char code of ENTER key
  • Implementing “partial void” in VB
  • Using Sax parsing to edit and write XML in VB6
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • What's the purpose of QString?
  • Debug.DrawLine not showing in the GameView
  • Content-Length header not returned from Pylons response
  • Yii2: Config params vs. const/define
  • Django rest serializer Breaks when data exists
  • How to rebase a series of branches?
  • Control modification in presentation layer
  • Projection media query: browser support and workarounds?
  • Azure Cloud Service Web Role web pages do not load
  • what is the difference between the asp.net mvc application and asp.net web application
  • PHP: When would you need the self:: keyword?