Dev1ce
Dev1ce

Reputation: 5944

NodeJs how to use MomentJs?

I am trying to get current date time in the following format -

2018-12-10T02:12:13.008391

I opted to use MomentJs, as many devs are suggesting it but
I'm unable to get a current date time the above format.

I tried the following code but I am unable to understand what the last part of the date time is after the .(dot)
The given datetime has 6 values after the .(dot),
But I am able to get only 4 values after the .(dot).

2019-03-19T10:18:20.1820

Can someone please help me with the correct Format and way to achieve my use case?

var moment = require('moment');
var datetime = moment().format('YYYY-MM-DDThh:mm:ss.ms');
console.log(datetime);

I tried referring the following link, but wasn't able to as I'm new to JS -
https://devhints.io/moment

Upvotes: 0

Views: 7959

Answers (1)

djheru
djheru

Reputation: 3719

const dateTime = moment().format('YYYY-MM-DDTHH:mm:ss.SSS000');

Moment will not be able to get you 6 decimal places for the time, because it's based on the built-in JavaScript Date object, which can only get you down to milliseconds. Your best bet would be to pad the time with zeroes if you really have to have 6 decimal places.

Upvotes: 2

Related Questions