Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

javascript - 在JavaScript中将日期转换为另一个时区(Convert date to another timezone in JavaScript)

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")

    (日期(格式为“ 2012/04/10 10:10:30 +0000”))

  • timezone string ("Asia/Jakarta")

    (时区字符串(“亚洲/雅加达”))

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

(时区字符串在http://en.wikipedia.org/wiki/Zone.tab中进行了描述)

Is there an easy way to do this?

(是否有捷径可寻?)

  ask by Rizky Ramadhan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For moment.js users, you can now use moment-timezone .

(对于moment.js用户,您现在可以使用moment-timezone 。)

Using it, your function would look something like this:

(使用它,您的函数将如下所示:)

function toTimeZone(time, zone) {
    var format = 'YYYY/MM/DD HH:mm:ss ZZ';
    return moment(time, format).tz(zone).format(format);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...