Convert date to another timezone in JavaScript

2018-12-31 01:32发布

I am looking for a function to convert date in one timezone to another.

It need two parameters,

  • date (in format "2012/04/10 10:10:30 +0000")
  • timezone string ("Asia/Jakarta")

The timezone string is described in http://en.wikipedia.org/wiki/Zone.tab

Is there an easy way to do this?

20条回答
爱死公子算了
2楼-- · 2018-12-31 01:45

Having looked around a lot including links from this page i found this great article, using moment timezone:

https://www.webniraj.com/2016/11/23/javascript-using-moment-js-to-display-dates-times-in-users-timezone/

To summarise it:

Get the user's timezone

var tz = moment.tz.guess();
console.info('Timezone: ' + tz);

Returns eg: Timezone: Europe/London

Set the default user timezone

moment.tz.setDefault(tz);

Set custom timezone

moment.tz.setDefault('America/Los_Angeles');

Convert date / time to local timezone, assumes original date/time is in UTC

moment.utc('2016-12-25 07:00').tz(tz).format('ffffd, Do MMMM YYYY, h:mma');

Returns: Sun, 25th December 2016, 7:00am

Convert date/time to LA Time

moment.utc('2016-12-25 07:00').tz('America/Los_Angeles').format('ffffd, Do MMMM YYYY, h:mma');

Returns: Sat, 24th December 2016, 11:00pm

Convert from LA time to London

moment.tz('2016-12-25 07:00', 'America/Los_Angeles').tz('Europe/London').format( 'ffffd, Do MMMM YYYY, h:mma' );

Returns: Sun, 25th December 2016, 3:00pm

查看更多
其实,你不懂
3楼-- · 2018-12-31 01:50

Got it !

Wanted to force the date shown = server date, no mattter the local settings (UTC).

My server is GMT-6 --> new Date().getTimezoneOffset() = 360.

myTZO = 360;
myNewDate=new Date(myOldDateObj.getTime() + (60000*(myOldDateObj.getTimezoneOffset()-myTZO)));
alert(myNewDate);
查看更多
孤独总比滥情好
4楼-- · 2018-12-31 01:51

Okay, found it!

I'm using timezone-js. this is the code:

var dt = new timezoneJS.Date("2012/04/10 10:10:30 +0000", 'Europe/London');
dt.setTimezone("Asia/Jakarta");

console.debug(dt); //return formatted date-time in asia/jakarta
查看更多
浮光初槿花落
5楼-- · 2018-12-31 01:51

I should note that I am restricted with respect to which external libraries that I can use. moment.js and timezone-js were NOT an option for me.

The js date object that I have is in UTC. I needed to get the date AND time from this date in a specific timezone('America/Chicago' in my case).

 var currentUtcTime = new Date(); // This is in UTC

 // Converts the UTC time to a locale specific format, including adjusting for timezone.
 var currentDateTimeCentralTimeZone = new Date(currentUtcTime.toLocaleString('en-US', { timeZone: 'America/Chicago' }));

 console.log('currentUtcTime: ' + currentUtcTime.toLocaleDateString());
 console.log('currentUtcTime Hour: ' + currentUtcTime.getHours());
 console.log('currentUtcTime Minute: ' + currentUtcTime.getMinutes());
 console.log('currentDateTimeCentralTimeZone: ' +        currentDateTimeCentralTimeZone.toLocaleDateString());
 console.log('currentDateTimeCentralTimeZone Hour: ' + currentDateTimeCentralTimeZone.getHours());
 console.log('currentDateTimeCentralTimeZone Minute: ' + currentDateTimeCentralTimeZone.getMinutes());

UTC is currently 6 hours ahead of 'America/Chicago'. Output is:

currentUtcTime: 11/25/2016
currentUtcTime Hour: 16
currentUtcTime Minute: 15

currentDateTimeCentralTimeZone: 11/25/2016
currentDateTimeCentralTimeZone Hour: 10
currentDateTimeCentralTimeZone Minute: 15
查看更多
春风洒进眼中
6楼-- · 2018-12-31 01:53

People familiar with the java 8 java.time package, or joda-time will probably love the new kid on the block: the js-joda library.

Install

npm install js-joda js-joda-timezone --save

Example

<script src="node_modules/js-joda/dist/js-joda.js"></script>
<script src="node_modules/js-joda-timezone/dist/js-joda-timezone.js"></script>
<script>
var dateStr = '2012/04/10 10:10:30 +0000';
JSJoda.use(JSJodaTimezone);
var j = JSJoda;
// https://js-joda.github.io/js-joda/esdoc/class/src/format/DateTimeFormatter.js~DateTimeFormatter.html#static-method-of-pattern
var zonedDateTime = j.ZonedDateTime.parse(dateStr, j.DateTimeFormatter.ofPattern('yyyy/MM/dd HH:mm:ss xx'));
var adjustedZonedDateTime = zonedDateTime.withZoneSameInstant(j.ZoneId.of('America/New_York'));
console.log(zonedDateTime.toString(), '=>', adjustedZonedDateTime.toString());
// 2012-04-10T10:10:30Z => 2012-04-10T06:10:30-04:00[America/New_York]
</script>

In true java nature, it's pretty verbose lol. But, being a ported java library, especially considering they ported 1800'ish test cases, it also probably works superbly accurately.

Chrono manipulation is hard. That's why many other libraries are buggy in edge cases. Moment.js seems to get timezones right, but the other js libs I've seen, including timezone-js, don't seem trustworthy.

查看更多
千与千寻千般痛.
7楼-- · 2018-12-31 01:53

Time Zone Offset for your current timezone

date +%s -d '1 Jan 1970'

For my GMT+10 timezone (Australia) it returned -36000

查看更多
登录 后发表回答