Use the utc() method of moment to remove the timezone and display everything in universal time.
moment.utc('2015-01-22T16:11:36.36-07:00').format('l LT')
That will display the time as it is in UTC without any timezone offset. If you want to display the time as it was recorded in the user/server timezone you can parse the zone information when you construct a moment instance and have it use the timezone recorded in the parsed string.
moment.parseZone('2015-01-22T16:11:36.36-07:00').format('l LT');
With either of these approaches you should consider labeling the time in some way to reflect the timezone the time corresponds to. Failing to do this could lead to a lot of confusion for the end users.
Answer from musicfuel on Stack OverflowUse the utc() method of moment to remove the timezone and display everything in universal time.
moment.utc('2015-01-22T16:11:36.36-07:00').format('l LT')
That will display the time as it is in UTC without any timezone offset. If you want to display the time as it was recorded in the user/server timezone you can parse the zone information when you construct a moment instance and have it use the timezone recorded in the parsed string.
moment.parseZone('2015-01-22T16:11:36.36-07:00').format('l LT');
With either of these approaches you should consider labeling the time in some way to reflect the timezone the time corresponds to. Failing to do this could lead to a lot of confusion for the end users.
You can use the utcOffset method to set the offset manually.
moment().utcOffset(0, true).format()
You can do this with moment.js using moment.utc().
http://momentjs.com/docs/#/parsing/utc/
moment([2011, 10, 8, 5]).format(); // different output based on timezone
moment.utc([2011, 10, 8, 5]).format(); // same output for all timezones
The way moment.utc works is by setting a flag internally to use getUTCMinutes instead of getMinutes, thus the output is the same in all timezones.
If you want your timezone completely ignored, you can use the following approach:
var firstDayStr = '29 January 2014';
var startAtTime = '10:01:02 AM';
var localFormat = 'YYYY-MM-DD[T]HH:mm:ss';
var m = moment(firstDayStr + ' ' + startAtTime).format(localFormat);
console.log(m);
Output:
2014-01-29T10:01:02
Have you tried parseZone?
moment.parseZone(end).format('MM/DD/YYYY');
That should keep your UTC offset applied. You can then also calculate the UTC offset, if you wanted to save that:
moment.parseZone(end).format('MM/DD/YYYY').utcOffset();
The solution I'm suggesting will ignore the timezone itself. It always take only the date. It might be a complex way to do it, but it always works for you.
const start = '2018-06-10T21:00:00-04:00'.split('-').slice(0, -1).join('-');
const end = '2018-06-10T23:00:00-04:00'.split('-').slice(0, -1).join('-');
const noconversion = moment(start).format('MM/DD/YYYY');
const converted = moment(end).format('MM/DD/YYYY');
Hi everyone,
I'm not familiar with javascript at all, only .Net and C++.
Here's the relevant piece of code that I'm working with:
this.timeStamp = moment('08:00', 'HHmm');
Of course, the time I put in isn't hardcoded but that's the exact format that the object I'm putting there contains. Every time.
Now, when I run this code, I receive an object containing the current date, information about my timezone (UTC+0300) and the time 11:00.
Judging from the documentation, this is seemingly normal behavior:
With Moment, the date is always interpreted as local time, unless you specify otherwise.
However, I can't seem to figure out how to specify otherwise. All I want is to create a moment-object that contains the exact time that I entered, not the time I entered + the offset from my local timezone.
It's probably a stupid question but I'm losing my mind over it.
I tried using a different format string, but that (obviously) didn't work. Using moment.utc() also just had the same result as moment().
Is anyone familiar with the moment library and could help me out here?
As pointed out in Manto's answer, .utcOffset() is the preferred method as of Moment 2.9.0. This function uses the real offset from UTC, not the reverse offset (e.g., -240 for New York during DST). Offset strings like "+0400" work the same as before:
// always "2013-05-23 00:55"
moment(1369266934311).utcOffset(60).format('YYYY-MM-DD HH:mm')
moment(1369266934311).utcOffset('+0100').format('YYYY-MM-DD HH:mm')
The older .zone() as a setter was deprecated in Moment.js 2.9.0. It accepted a string containing a timezone identifier (e.g., "-0400" or "-04:00" for -4 hours) or a number representing minutes behind UTC (e.g., 240 for New York during DST).
// always "2013-05-23 00:55"
moment(1369266934311).zone(-60).format('YYYY-MM-DD HH:mm')
moment(1369266934311).zone('+0100').format('YYYY-MM-DD HH:mm')
To work with named timezones instead of numeric offsets, include Moment Timezone and use .tz() instead:
// determines the correct offset for America/Phoenix at the given moment
// always "2013-05-22 16:55"
moment(1369266934311).tz('America/Phoenix').format('YYYY-MM-DD HH:mm')
A couple of answers already mention that moment-timezone is the way to go with named timezone. I just want to clarify something about this library that was pretty confusing to me. There is a difference between these two statements:
moment.tz(date, format, timezone)
moment(date, format).tz(timezone)
Assuming that a timezone is not specified in the date passed in:
The first code takes in the date and assumes the timezone is the one passed in. The second one will take date, assume the timezone from the browser and then change the time and timezone according to the timezone passed in.
Example:
moment.tz('2018-07-17 19:00:00', 'YYYY-MM-DD HH:mm:ss', 'UTC').format() // "2018-07-17T19:00:00Z"
moment('2018-07-17 19:00:00', 'YYYY-MM-DD HH:mm:ss').tz('UTC').format() // "2018-07-18T00:00:00Z"
My timezone is -5 from utc. So in the first case it does not change and it sets the date and time to have utc timezone.
In the second case, it assumes the date passed in is in -5, then turns it into UTC, and that's why it spits out the date "2018-07-18T00:00:00Z"
NOTE: The format parameter is really important. If omitted moment might fall back to the Date class which can unpredictable behaviors
Assuming the timezone is specified in the date passed in:
In this case they both behave equally
Even though now I understand why it works that way, I thought this was a pretty confusing feature and worth explaining.
See moment issue #887, directly regarding this. It may be easier in a future version, but the current workaround is as follows:
var input = "2013-09-20 23:59:59 +0100";
var m = moment(input).zone(input);
m.format("DD-MM-YYYY HH:mm:ss ZZ")
In moment.js v-2.8.3:
var dateTime = "2014-12-09 13:59:59 +0930";
var parseDateTimeZone = moment.parseZone(dateTime).format("YYYY-MM-DDTHH:mm:ssZ");
Doc-API