Same date in different time zone

前端 未结 4 2067
小鲜肉
小鲜肉 2020-12-02 17:18

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 resul         


        
相关标签:
4条回答
  • 2020-12-02 17:30

    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"
    
    0 讨论(0)
  • 2020-12-02 17:32

    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
    
    0 讨论(0)
  • 2020-12-02 17:44

    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);
    
    0 讨论(0)
  • 2020-12-02 17:56

    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()
    
    0 讨论(0)
提交回复
热议问题