参考代码如下:
<SCRIPT LANGUAGE = "JavaScript">var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP.3.0")
xmlhttp.open("GET", "http://bjtime.cn", false)
xmlhttp.setRequestHeader("If-Modified-Since", "bjtime")
xmlhttp.send()
var dateStr = xmlhttp.getResponseHeader("Date")
var date = new Date(dateStr)
var year = date.getFullYear()
var month = date.getMonth() + 1
var date1 = date.getDate()
var hour = date.getHours()
var minutes = date.getMinutes()
var second = date.getSeconds()
alert(date + " | " + year + "年" + month + "月" + date1 + "日" + hour + "时" + minutes + "分" + second + "秒")
</SCRIPT>
即构造了一个日期,获取时间戳(getTime)再获取本地时间偏移(getTimezoneOffset),然后把原时间减去该偏移,再重新构造一个新时间即可。如果本地系统如Ubuntu,用的就是UTC时间,那么getTiezoneOffset()将返回0,上面的过程就失效了。
如果要强制转成北京时间,也很简单,就是硬编码-28800000
像这样:
>a = new Date(new Date().getTime() + 28800000)2017-01-18T17:34:31.296Z