4

I am working on a script that needs to retrieve and store a timestamp from a database. The timestamp from the database is retrieved as a UTC Zulu formatted string "YYYY-MM-DDTHH:MM:SS.SSSZ". I want to store this timestamp in the format "YYYY-MM-DDTHH:MM:SS.SSS" in local time. I am currently attempting to do this with moment.js, I have had little success with ".toISOString()" and other methods.

However, I have noticed that the output from "moment(timestamp).format()" does not return the string I am expecting from what I understand so far about UTC. Here is a code example that replicates my issue:

var moment = require('moment');

var timestamp = new Date('2018-05-30T15:01:01.111Z');

console.log(timestamp);
console.log(moment(timestamp).format('YYYY-MM-DDTHH:mm:ss.sss'));
console.log(moment(timestamp).format('YYYY-MM-DDTHH:MM:SS.SSS'));

This is my output:

2018-05-30T15:01:01.111Z
2018-05-30T16:01:01.011
2018-05-30T16:05:11.111

This is my expected out:

2018-05-30T15:01:01.111Z
2018-05-30T16:01:01.111
2018-05-30T16:01:01.111

Why does the change in case from 'YYYY-MM-DDTHH:mm:ss.sss' to 'YYYY-MM-DDTHH:MM:SS.SSS' in .format() cause a different output? Is my expected output correct or have a misunderstood moment.(timestamp).format()? Lastly, is there a better way to achieve my expected output given the circumstances?

Todd
  • 758
  • 1
  • 9
  • 20
  • 3
    `mm` is minutes `MM` is months – Joe Warner May 30 '18 at 16:36
  • 1
    How about reading the [documentation](http://momentjs.com/docs/#/displaying/format/)? 1. There's no `sss` format, 2. The uppercase version `S`, `SS`, ... returns fractional seconds, hence milliseconds, ..., 3. `MM` returns the month – Andreas May 30 '18 at 16:36
  • Be careful `new Date()` will change your timestamp to your local timestamp. – RajnishCoder Jun 24 '21 at 11:32

1 Answers1

8

http://momentjs.com/docs/#/displaying/format/

MM is months thats why you got 2018-05-30T16:05:11.111 a 5

there is no sss but there is a SSS

you said you want in this format "YYY-MM-DDTHH:MM:SS.SSS" I assume MM you mean minutes due to your expected outcome. This is an odd way to store a date as there are no months and it's repeating seconds.

I'd suggest storing in UTC.

var timestamp = new Date('2018-05-30T15:01:01.111Z');

console.log(timestamp);
console.log(moment(timestamp).format('YYYY-MM-DDTHH:mm:ss:SSS'));

// i'd suggest UTC over formating but if you were i'd use
console.log(moment(timestamp).format('YYYY-MM-DDTHH:MM:mm:SSS'));
//2018-05-30T15:01:01.111Z
//2018-05-30T16:01:01.111
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.js"></script>

Please let me know if this how you want it.

Joe Warner
  • 3,335
  • 1
  • 14
  • 41
  • Thanks Joe, this is what I needed. I had not spotted that MM represented months, and mm represented minutes. I would like to store my timestamp in UTC unformatted however the npm package odbc that I am using seems to have issues when I query a database with a UTC timestamp. I have needed to format the stamp to get around this issue. Thank for your time Joe! – Todd May 30 '18 at 17:02
  • No worries, glad to help. whats the issue you're having about querying i'd try solve that instead of this if possible :) have a nice day – Joe Warner May 30 '18 at 17:03
  • Thank you for being so helpful. It seems to be that the npm package odbc I am using returns my timestamps in the format "2018-05-30T15:35:57.000Z" as an example. I do not know if this is node odbc or the sybase odbc driver on my server causing this. Ironically, though I receive timestamps in this format, if I query the database with them in this format it returns an error. Equally query the databse with this timestamp in UTC returns values from an hour behind local, as if it is not being converted correctly. It has left me with this current method, though not perfect, working with your help =) – Todd May 30 '18 at 17:15
  • I'd imagine if they're an hour before its because the servers timezone is differenent and the offset will sort but i may be wrong – Joe Warner May 30 '18 at 17:49
  • 2
    How do I get a Zulu timestamp format like 2018-05-30T15:01:01.111Z - console.log(moment().utc().format('YYYY-MM-DDTHH:MM:mm:ss.SSSZ'));//returns 2018-05-30T15:01:01.111+00:00 – Vijay Mar 22 '19 at 22:52
  • Z at the end is short for Zulu and means UTC. – Joe Warner Mar 23 '19 at 13:24
  • 4
    @Vijay - Try .format('YYYY-MM-DDTHH:mm:ss.SSS[Z]'). [] are the Escaping characters and Z will be appended as is after formatting. – jsmtslch Jul 12 '19 at 20:54