Reputation: 446
I am trying to send a datetime using a datetimepicker. however it seems the datepicker has a unique date format assigned to it and i am unsure how to format it using moment js. The datetime is "2016-04-12T03:52:57.0450971+01:00".
Upvotes: 1
Views: 4546
Reputation: 2452
I'm not sure if you're attempting to pass that string into your datepicker, or to format it after getting it from your datepicker. In either case, it is a ISO8601 format date, which is the primary standard for date formats in modern software. If you need to parse that date IN to moment, you need only use the default constructor:
moment('2016-04-12T03:52:57.0450971+01:00')
Note that when you use the default constructor, moment will convert the date from the specified offset to your local time in the browser. If you would like to keep the date as a fixed offset, use parseZone instead:
moment.parseZone('2016-04-12T03:52:57.0450971+01:00')
If you would like to parse the date in UTC, use the UTC function. This will convert the date from the specified offset to UTC.
moment.utc('2016-04-12T03:52:57.0450971+01:00')
If you need to get a date string OUT of moment in that format, and you do not need milliseconds, you can just call .format directly:
moment().format()
If you need milliseconds (so that exact format), you can use the following format tokens:
moment().format('YYYY-MM-DDTHH:mm:ss.SSSSSSSZ')
Note that Moment only takes fractional seconds out to three significant digits. All subsequent digits will be zero-padded.
Upvotes: 4