使用JavaScript获取特定时区的日期时间 [英] Get date time for a specific time zone using JavaScript

查看:175
本文介绍了使用JavaScript获取特定时区的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎JavaScript的Date()函数只能返回本地日期和时间。有没有办法获得特定时区的时间,例如GMT-9?

It seems that JavaScript's Date() function can only return local date and time. Is there anyway to get time for a specific time zone, e.g., GMT-9?

结合@ Esailija和@ D3mon-1stVFW,我想出了:你需要有两个时区偏移,一个用于本地时间,一个用于目的地时间,这里是工作代码:

Combining @​Esailija and @D3mon-1stVFW, I figured it out: you need to have two time zone offset, one for local time and one for destination time, here is the working code:

var today = new Date();  
var localoffset = -(today.getTimezoneOffset()/60);
var destoffset = -4; 

var offset = destoffset-localoffset;
var d = new Date( new Date().getTime() + offset * 3600 * 1000)


$ b $例如: http://jsfiddle.net/BBzyN/3/

推荐答案

var offset = -8;
new Date( new Date().getTime() + offset * 3600 * 1000).toUTCString().replace( / GMT$/, "" )

"Wed, 20 Jun 2012 08:55:20"

这篇关于使用JavaScript获取特定时区的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆