Same date in different time zone

2019-01-10 21:11发布

问题:

My question is how can I get the same day, month, year, hour, minutes, seconds in a different time zone, for example:

var now = moment().valueOf();
var result1 = moment(now).format('DD-MM-YYYY HH:mm:SS Z');

In my time zone I get some this like this:

18-02-2015 21:08:34 +01:00

So how can I change only time zone without changing other values (days, months, ..., minutes, ...)

I want to get some thing like this:

    result2: 18-02-2015 21:08:34 +01:00
    result3: 18-02-2015 21:08:34 +10:00
    result4: 18-02-2015 21:08:34 +05:00
    result5: 18-02-2015 21:08:34 -06:00
    result6: 18-02-2015 21:08:34 -11:00

Thanks in advance

回答1:

Here's how you could do what you are asking:

// get a moment representing the current time
var now = moment();

// create a new moment based on the original one
var another = now.clone();

// change the offset of the new moment - passing true to keep the local time
another.utcOffset('+05:30', true);

// log the output
console.log(now.format());      // "2016-01-15T11:58:07-08:00"
console.log(another.format());  // "2016-01-15T11:58:07+05:30"

However, you must recognize two important things:

  • The another object no longer represents the current time - even in the target time zone. It's a completely different moment in time. (The world does not synchronize local clocks. If it did, we'd have no need for time zones!).

    For this reason, even though the above code satisfies the question that was asked, I strongly recommend against using it. Instead, re-evaluate your requirements, as it's likely they are misunderstanding the nature of time and time zones.

  • A time zone cannot be fully represented by an offset alone. Read "Time Zone != Offset" in the timezone tag wiki. While some time zones have fixed offsets (such as +05:30 used by India), many time zones change their offsets at different points throughout the year to accommodate daylight saving time.

  • If you wanted to account for this, you could use moment-timezone instead of calling utcOffset(...). However, the issue in my first bullet would still apply.

// get a moment representing the current time
var now = moment();

// create a new moment based on the original one
var another = now.clone();

// change the time zone of the new moment - passing true to keep the local time
another.tz('America/New_York', true); // or whatever time zone you desire

// log the output
console.log(now.format());      // "2016-01-15T11:58:07-08:00"
console.log(another.format());  // "2016-01-15T11:58:07-05:00"


回答2:

The most-voted answer is messy IMO. Here's a cleaner solution - similar to BlueSam's answer, but safer:

const myTime = moment.tz('2016-08-30T22:00:00', moment.ISO_8601, 'America/Denver')

myTime.format() //2016-08-30T22:00:00-06:00

const sameTimeDifferentZone = moment.tz(myTime.format('YYYY-MM-DDTHH:mm:ss.SSS'), moment.ISO_8601, 'America/New_York')

sameTimeDifferentZone.format() //2016-08-30T22:00:00-04:00


回答3:

After reading the above comments, I thought I'd add in based on Joao's answer. In my case I was trying to use a preexisting moment date with a timezone and converting it to another timezone while retaining the original date value (as asked in the question).

var newTimezone = 'America/Denver';

//date - contains existing moment with timezone i.e 'America/New_York'
moment.tz(date.format('YYYY-MM-DDTHH:mm:ss'), 'YYYY-MM-DDTHH:mm:ss', newTimezone);


回答4:

From the moment docs: http://momentjs.com/timezone/docs/

reference moment-timezone-with-data.js and specify which timezone to go to, like so:

moment(date).tz("America/Los_Angeles").format()