0

I am using moment.js in my application and the expected date format is

2017-01-09T17:05:00.000 //Expected Result

Where as if i call

moment().format() 

I am getting ISO 8601 format i.e with T and minus sign

(2017-01-14T17:05:00-06:00) // Actual result.

What should i use to get this format of with .000

Alaksandar Jesus Gene
  • 6,523
  • 12
  • 52
  • 83

1 Answers1

0

ISO 8601 is the default representation of moment's format() API. But you can customize it by passing required format pattern.

In your case, moment().format('YYYY-MM-DD[T]HH:mm:ss.SSS') will produce what you're looking

2017-01-10T11:55:56.621

Find more customization options here: http://momentjs.com/docs/#/displaying/

--

Another option to know, though it works only for UTC time : moment().toISOString()

2017-01-10T06:56:18.465Z

[credit: Rajesh]

Jay Kumar
  • 1,514
  • 1
  • 14
  • 17
  • Thanks Bro. Saved my day. – Alaksandar Jesus Gene Jan 10 '17 at 06:37
  • This is not the idea way. Please check the reference link in my last comment – Rajesh Jan 10 '17 at 06:38
  • @Rajesh Are you refering to `console.log(moment().toISOString())` in your jsfiddle ? – Jay Kumar Jan 10 '17 at 06:59
  • @JayKumar I meant the comment where I marked this question as duplicate. That post address the correct way i.e. `,toISOString()` – Rajesh Jan 10 '17 at 07:04
  • @Rajesh There's a catch in it. From official docs: `Note that .toISOString() always returns a timestamp in UTC, even if the moment in question is in local mode. This is done to provide consistency with the specification for native JavaScript Date .toISOString(), as outlined in the ES2015 specification.` – Jay Kumar Jan 10 '17 at 07:06
  • Are you sure? Please check the fiddle. Also if this was true, it would have been a major fuckup by moment team and would have been addressed by now. `.format()` without any format would give utc format, but if you do `.toISOString()` this will return ISO format. If it does not, please raise an issue. – Rajesh Jan 10 '17 at 07:10
  • Yes. It's true and I've quoted the official docs from here. http://momentjs.com/docs/#/displaying/as-iso-string/ Note that .toISOString() always returns a timestamp in UTC, even if the moment in question is in local mode. This is done to provide consistency with the specification for native JavaScript Date .toISOString(), as outlined in the ES2015 specification. – Jay Kumar Jan 10 '17 at 07:23