<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>example</title>
<script language="JavaScript" type="text/javascript">
var localTime = new Date(2010,09,10);
var localT=new Date(2010,0,10);
//alert(localT。toUTCString());
document.write(localTime);
</script>
</head>
<body>
<h4>
UTC Time is
<script language="JavaScript" type="text/javascript">
document.write(localTime.toUTCString());
</script>
</h4>
<h4>
Local Time is
<script language="JavaScript" type="text/javascript">
document.write(localTime.toLocaleString());
</script>
</h4>
<h4>
Time Zone Offset is
<script language="JavaScript" type="text/javascript">
document.write(localTime.getTimezoneOffset());
</script>
</h4>
<h4>
Using toLocalTimeString() gives:
<script language="JavaScript" type="text/javascript">
if (localTime.toLocaleTimeString)
{
document.write(localTime.toLocaleTimeString())
}
</script>
</h4>
<h4>
Using toTimeString() gives:
<script language="JavaScript" type="text/javascript">
if (localTime.toTimeString)
{
document.write(localTime.toTimeString() )
}
</script>
</h4>
<h4>
Using toLocaleDateString() gives:
<script language="JavaScript" type="text/javascript">
if (localTime.toLocaleDateString)
{
document.write(localTime.toLocaleDateString())
}
</script>
</h4>
<h4>
Using toDateString() gives:
<script language="JavaScript" type="text/javascript">
if (localTime.toDateString)
{
document.write(localTime.toDateString())
}
</script>
</h4>
</body>
</html>
可能您还没有领会到,Date类对UTC日期和时间有很强的依赖性。Date类用方法getTimezoneOffset()来说明某个时区与UTC时间的关系,该方法返回当前时区比UTC提前或落后的分钟数。
例如,对于U.S Eastern Daylight Saving Time(美国东部夏令时),getTime-zoneOffset()返回300,即比UTC时间落后5个小时(300分钟)。
还可用getTimezoneOffset()方法判断时区使用的是否是夏令时。实现这一点需要创建任意年份的1月1日的日期,然后创建该年份的7月1日的日期,比较时区偏移量。如果分钟数不等,说明该时区使用的是夏令时,如果相等,则该时区使用的不是夏令时。
var di = new Date(2009,0,1);
var d2 = new Date(2009,6,1);
var Time = d1.getTimezoneOffset != d2.getTimezoneOffset();