如何在Javascript中的时区之间转换

时间:2018-06-16 14:55:12

标签: javascript time timezone

我正在建立一个比分直播,但我正在使用的API是我当地时间之前2小时的返回时间,我无法将其转换为我国的时间,因为来自不同国家的不同用户将访问实时比分。我不知道api时区..但我想要一个能获得api时区并转换为用户当地时间的函数

2 个答案:

答案 0 :(得分:0)

对于http://momentjs.com用户,您现在可以使用

http://momentjs.com/timezone/docs/

使用它,你的功能看起来像这样:

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

答案 1 :(得分:0)

你可以使用momentjs。例子小提琴`

var serverdate = "Fri Jun 15 2018 08:03:23 GMT-0700 (Pacific Daylight Time)"

var date3 =  moment(serverdate) // this will automatically convert the date to the time zone on  user's machine

alert(date3)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>

`