Using moment.js, I'm attempting to extract the offset from an ISO date string so I can use the offset later when formatting an epoch timestamp to ensure the conversion of the timestamp is in the same timezone.
Even though the offset in the string is -0400, the result is always 0;
var currentTime = "2015-03-18T16:10:00.001-0400";
var utcOffset = moment(currentTime).utcOffset(); // 0
I've attempted to use parseZone() as well without success. Is there a way to extract -0400 from the string so I can use it while formatting another time?
Thanks for the help! KC
utcOffset(480); moment#utcOffset will search the string for the last match of +00 -00 +00:00 +0000 -00:00 -0000 Z , so you can even pass an ISO8601 formatted string with offset and the moment will be changed to that UTC offset. Note that if the string does not include 'Z', it must include the + or - character.
Moment construction falls back to js Date. This is discouraged and will be removed in an upcoming major release. This deprecation warning is thrown when no known format is found for a date passed into the string constructor.
To get the current date and time, just call moment() with no parameters. var now = moment(); This is essentially the same as calling moment(new Date()) . Note: From version 2.14.0, moment([]) and moment({}) also return now.
The moment(). hour() Method is used to get the hours from the current time or to set the hours. moment().
The format method also lets us format a date to an ISO-8601 date string. Then we get the same result as before. We can use the Moment Timezone library to add support for time zones. Then we can set the time zone in our app and then use the toISOString method to convert the moment object to an ISO-8601 string.
Note: Unlike moment.fn.zone this function returns the real offset from UTC, not the reverse offset (as returned by Date.prototype.getTimezoneOffset ). Getting the utcOffset of the current object: Setting the UTC offset by supplying minutes. The offset is set on the moment object that utcOffset () is called on.
moment ().utcOffset (); moment ().utcOffset (Number|String); moment ().utcOffset (Number|String, Boolean); Get or set the UTC offset in minutes. Note: Unlike moment.fn.zone this function returns the real offset from UTC, not the reverse offset (as returned by Date.prototype.getTimezoneOffset ).
Moment does allow for offsets to be passed as strings, but it expects them to be in one of the ISO8601 formats: either [+/-]HH:mm or [+/-]HHmm. Additionally, note that I used moment.utc (...) to parse the input string.
The correct way to extract the offset is indeed with parseZone
var currentTime = "2015-03-18T16:10:00.001-0400";
var utcOffset = moment.parseZone(currentTime).utcOffset();
This should result in -240
, which means 240 minutes behind UTC, which is the same as the -0400
in the input string. If you wanted the string form, instead of utcOffset()
you could use .format('Z')
for "-04:00"
or .format('ZZ')
for "-0400"
.
The form you gave in the question just uses the computer's local time zone. So it is currently UTC+00:00 in your time zone (or wherever the code is running), that would explain why you would get a zero. You have to use parseZone
to retain the offset of the input string.
Also - your use case is a bit worrying. Remember, an offset is not the same thing as a time zone. A time zone can change its offset at different points in time. Many time zones do this to accommodate daylight saving time. If you pick an offset off of one timestamp and apply it to another, you don't have any guarantees that the offset is correct for the new timestamp.
As an example, consider the US Eastern time zone, which just changed from UTC-05:00 to UTC-04:00 when daylight saving time took effect on March 8th, 2015. If you took a value like the one you provided, and applied it to a date of March 1st, you would be placing it into the Atlantic time zone instead of the Eastern time zone.
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