I have a string like "10/14/2014 5:30:00 AM"
. I need to parse it as a UTC string. But when I execute moment.utc("10/14/2014 5:30:00 AM").format()
I get "2014-10-13T22:30:00+00:00"
while I expected to get "2014-10-14T12:30:00+00:00"
.
How can I make momentJS believe my string a UTC string and parse it without shifting time according to local timezone (+7 in my case)?
Java 8 – Parse string to date-time in UTC 1 Instant, OffsetDateTime and ZonedDateTime#N#In Java 8, OffsetDateTime, ZonedDateTime and Instant – all store an instant... 2 Parse string to OffsetDateTime in UTC#N#Date time with offset information is represented in... 3 Parse string to ZonedDateTime in UTC More ...
As other answers point out, the UTC date string with Z designator would indeed be successfully parsed, but it would also be converted to local time, i.e. it returns DateTime with a Kind of Local and adjusted timestamp.
Parse a date and time string by using the conventions of a specific culture. Parse a date and time string with special style elements (such as white space or no white space). Parse a date and time string that must be in a particular format. Parse a date and time string and perform a conversion to UTC or local time.
‘Z’ in string represents the UTC timezone. It is short form of Zulu and can be written as UTC +0:00. Program output. 3. Parse String to ZonedDateTime in UTC Date time with full zone information is represented in the following formats. dd/MM/uuuu’T’HH:mm:ss:SSSXXXXX pattern. e.g. "03/08/2019T16:20:17:717+05:30".
This is indeed a bug, and I filed it here. The problem occurs whenever you allow moment to fall back to the browser's parser. If you watch in your browser's debug console when you run this code, you'll see the following warning:
Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.
The warning is normal, telling you not to use moment in a way that falls back to to the JavaScript Date
object's parser. You can avoid this by providing a format string.
In general, passing values in m/d/y format without a format string is a bad idea, because not all users will have that format setting in their browser. A value like 1/4/2014 could be interpreted as January 4th, or as April 1st.
To explicitly tell moment to parse in a particular format, pass the format string like this:
moment.utc("10/14/2014 5:30:00 AM", "M/D/YYYY h:mm:ss a")
I find the exact same behavior which seems like a bug to me since utc() shouldn't infer a timezone when one isn't present.
The solution is to re-format your date before calling utc()
. I find that the array syntax and ISO 8601 format work fine:
// Array format - Works
moment.utc([2014, 10, 14, 10, 05]).format()
// Nope
moment.utc("10/14/2014 5:30:00 AM").format();
// ISO 8601 - Works
moment.utc("2014-10-14 05:30:00").format();
JSFiddle: http://jsfiddle.net/0qw54nrb/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With