JavaScript Date 对象的 toString 相关

作者:vkvi 来源:ITPOW(原创) 日期:2009-8-11

JavaScript Date 对象中与转换成字符串格式的相关函数比较多,列出来:

var dt = new Date();
document.write(dt.toGMTString() + "<br />")
document.write(dt.toUTCString() + "<br />")

document.write(dt.toString() + "<br />")
document.write(dt.toLocaleString() + "<br />")

document.write(dt.toDateString() + "<br />")
document.write(dt.toLocaleDateString() + "<br />")

document.write(dt.toTimeString() + "<br />")
document.write(dt.toLocaleTimeString() + "<br />")

结果类似如下:

Tue, 11 Aug 2009 11:15:33 UTC
Tue, 11 Aug 2009 11:15:33 UTC
Tue Aug 11 19:15:33 UTC+0800 2009
2009年8月11日 19:15:33
Tue Aug 11 2009
2009年8月11日
19:15:33 UTC+0800
19:15:33

toGMTString() 和 toUTCString() 功能相同,注意,由于中国处于东八区,所以这里其时间有 8 小时的变化。虽然二者功能相同,但推荐使用 toUTCString(),因为现在多说 UTC 时间,而没说 GMT 时间

后面几个,都是成对出现的,也望名生义,其中带 Locale 的结果取决于客户端计算机区域设置。注意是 Locale,而不是 Local

相关文章