Moment-timezone: moment().toDate() is returns the Date with the system Time Zone

Created on 28 Jun 2018  路  4Comments  路  Source: moment/moment-timezone

moment().toDate() is returns the Date with the system timezone.
Is there any other way to get date object with custom timezone.

Most helpful comment

moment().tz('America/New_York').toString()

will return you date & time for New York timezone, but .toDate() returns JavaScript Date object which has no timezone information and is always displayed in system timezone.

All 4 comments

moment().tz('America/New_York').toString()

will return you date & time for New York timezone, but .toDate() returns JavaScript Date object which has no timezone information and is always displayed in system timezone.

Thanks @ellenaua . I want to create a Date object with particular timezone is it possible?
Ex.
Convert_UTCDate_To_LocalDate(utcTime: Date, localTimeZone: string) {
var utcMoment = moment(utcTime);
var localDateTime = utcMoment.tz(localTimeZone);

return new Date(localDateTime.format('lll'));  //returns java script date it contains system time zone not 'localTimeZone'

}

It's impossible, JavaScript Date object does not store any timezone information (it contains unix timestamp inside, and when you call .getMinutes (), getHours (), toString () it uses system timezone). That's why moment-timezone was created: to have Date objects which also have timezone info.

Thanks for your time @ellenaua

Was this page helpful?
0 / 5 - 0 ratings