物件 Date 的一系列 to 方法。在不同瀏覽器,輸出結果會有不同。
toDateString | toGMTString | toLocaleDateString | toLocaleFormat |
toLocaleString | toLocaleTimeString | toSource | toString |
toTimeString | toUTCString | valueOf |
方法 toDateString() 將物件實體的日期部分,轉換成可讀的字串。
- 程式用法:
<script type='text/javascript'>
var today=new Date();
document.write( today.toDateString() );
</script> - 執行結果:
Tue Apr 01 2025
方法 toGMTString() 將物件實體,依據 GMT 轉換成可讀的字串。已廢止,請改用 toUTCString()。
- 程式用法:
<script type='text/javascript'>
document.write( today.toGMTString() );
</script> - 執行結果:
Tue, 01 Apr 2025 22:38:59 GMT
方法 toLocaleDateString() 將物件實體的日期部分,依據用戶電腦系統的日期格式,轉換成可讀的字串。
- 程式用法:
<script type='text/javascript'>
document.write( today.toLocaleDateString() );
</script> - 執行結果:
4/1/2025
方法 toLocaleFormat() 將物件實體,依據格式字串,轉換成可讀的字串。此法只有 Firefox 有支援;IE, Safari, Chrome 無此項。建議勿用。
- 程式用法:
<script type='text/javascript'>
document.write( today.toLocaleFormat( '%Y-%m-%d' ) );
</script> - 執行結果:
方法 toLocaleString() 將物件實體,依據用戶電腦系統的格式,轉換成可讀的字串。
- 程式用法:
<script type='text/javascript'>
document.write( today.toLocaleString() );
</script> - 執行結果:
4/1/2025, 10:38:59 PM
方法 toLocaleTimeString() 將物件實體的時分部分,依據用戶電腦系統的時分格式,轉換成可讀的字串。
- 程式用法:
<script type='text/javascript'>
document.write( today.toLocaleTimeString() );
</script> - 執行結果:
10:38:59 PM
方法 toSource() 回傳物件實體的源碼。此法只有 Firefox 有支援;IE, Safari, Chrome 無此項。建議勿用。
- 程式用法:
<script type='text/javascript'>
document.write( today.toSource() );
</script> - 執行結果:
方法 toString() 將物件實體,轉換成可讀的字串。
- 程式用法:
<script type='text/javascript'>
document.write( today.toString() );
</script> - 執行結果:
Tue Apr 01 2025 22:38:59 GMT+0000 (Coordinated Universal Time)
方法 toTimeString() 將物件實體的時分部分,轉換成可讀的字串。
- 程式用法:
<script type='text/javascript'>
document.write( today.toTimeString() );
</script> - 執行結果:
22:38:59 GMT+0000 (Coordinated Universal Time)
方法 toUTCString() 將物件實體,依據 UTC 轉換成可讀的字串。
- 程式用法:
<script type='text/javascript'>
document.write( today.toUTCString() );
</script> - 執行結果:
Tue, 01 Apr 2025 22:38:59 GMT
方法 valueOf() 回傳物件實體總微秒數(自 1970/1/1 0:0:0 算起)。
- 程式用法:
<script type='text/javascript'>
document.write( today.valueOf() );
</script> - 執行結果:
1743547139026