筆記一下Date物件的相關用法
1. 得到目前的時間:
// local time
var dt = new Date();
document.write('current local time: '+dt);
// GMT time
document.write('current GMT time: '+dt.toGMTString());
// UTC time
document.write('current UTC time: '+dt.toUTCString());
// UTC time to local time
document.write('get current local time from UTC time: '+(new Date(dt.toUTCString())));
2. 計算兩個日期所差的天數:
var date1 = new Date(2012,8,1);
var date2 = new Date(2012,9,1);
document.write("diff time: "+(date2-date1)/(1000*60*60*24));
3. get time:
// new Date(year, month, day [, hour, minute, second, millsecond])
var dt = new Date(2012,1,1);
document.write('Date: '+dt+'<br/>');
document.write('Year: '+dt.getFullYear()+'<br/>');
document.write('Month: '+dt.getMonth()+'<br/>');
document.write('Day: '+dt.getDate()+'<br/>');
document.write('Week: '+dt.getDay());
最後筆記一下GMT和UTC的差別,基本上在通常的情況下是差不多的,GMT代表的是格林威治標準時間,UTC也是以格林威治為標準的時間,不過差別在UTC是以原子鐘來做校準,在通常的狀況下GMT和UTC的精準度是沒有差別的(可以參考:時區概念(24時區、GMT、UTC))
Reference:
沒有留言:
張貼留言