Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same date in different time zone

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

like image 521
Zouhair Avatar asked Feb 18 '15 20:02

Zouhair


People also ask

Does timezone affect date?

Timezones only affect the human readable representation of the point in time. are both the exact same point in absolute time. It's just two different human ways of describing the same instant.

Can different time zones be the same time?

In fact, many countries continue to set their own times today. For example, in China, it's always the same time all across the country. That's despite the fact that China stretches across three standard time zones.

What happens when you enter a different time zone?

When you travel to another time zone, there's a dramatic shift in your exposure to light and misalignment of your body's sense of day and night. The sudden disruption of your circadian rhythms that occurs with jet lag can be distressing, especially the further you travel.


2 Answers

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" 
like image 78
Matt Johnson-Pint Avatar answered Oct 05 '22 16:10

Matt Johnson-Pint


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 
like image 44
Joao Avatar answered Oct 05 '22 16:10

Joao