如果你连接的服务器bjtime.cn没有在response响应里给你设置Date,则会取日期初始化的时间,也就是UTC0或者GMT0。
你在var d = new Date(dateStr)之前把dateStr给alert出来看看是否正确?
参考代码如下:
<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>
洛杉矶时区是西八区,代码:var sq=-8 //设置时区,东为正数,西为负数
var d = new Date()
var utc = d.getTime() + (d.getTimezoneOffset() * 60000)
var nd = new Date(utc + (3600000*sq))
alert("北京时间:"+d.toLocaleString()+"\r\r洛杉矶时间:"+nd.toLocaleString())