Moment.js not formatting time in microseconds

4.1k Views Asked by At

I'm using moment.js to format a datetime value as follows:

time = moment(this.params['timeValue']).format("HHmmss.SSS")

Here, this.params['timeValue'] is a string. An example value is 2020-02-05 10:00:00.123 . formatting using moment returns the value 100000.123. Sometimes my datetime value goes to microsecond value, but moment js seems to cut the formatted value to millisecond level. For example formatting 2020-02-05 10:00:00.123456 returns 100000.123. I've tried this, but it did not work:

time = moment(this.params['timeValue']).format("HHmmss.SSSSSS")

Could you help me how to handle the formatting of a datetime value with microseconds. If moment.js doesn't handle it, is there any other library I could use?

1

There are 1 best solutions below

0
On

Moment.js wraps the native date type, which goes down to only milliseconds. You will only get the first three digits, the moment.js documentation shows an example of the fractional seconds (as per your second code snippet), but also mentions that it will only display the 3 significant digits, filling the rest with zeros.

Check out this SO question for more information about microseconds in JavaScript.