[JavaScript基礎(chǔ)]學(xué)習(xí)②①--Date

var now = new Date();
now; // Wed Jun 24 2015 19:49:22 GMT+0800 (CST)
now.getFullYear(); // 2015, 年份
now.getMonth(); // 5, 月份,注意月份范圍是0~11,5表示六月
now.getDate(); // 24, 表示24號(hào)
now.getDay(); // 3, 表示星期三
now.getHours(); // 19, 24小時(shí)制
now.getMinutes(); // 49, 分鐘
now.getSeconds(); // 22, 秒
now.getMilliseconds(); // 875, 毫秒數(shù)
now.getTime(); // 1435146562875, 以number形式表示的時(shí)間戳

創(chuàng)建一個(gè)指定日期和時(shí)間的Date對(duì)象

var d = new Date(2015, 5, 19, 20, 15, 30, 123);
d; // Fri Jun 19 2015 20:15:30 GMT+0800 (CST)

創(chuàng)建一個(gè)符合[ISO 8601]格式的字符串

var d = Date.parse('2015-06-24T19:49:22.875+08:00');
d; // 1435146562875 時(shí)間戳
var d = new Date(1435146562875);
d; // Wed Jun 24 2015 19:49:22 GMT+0800 (CST)

時(shí)區(qū)

var d = new Date(  );
d.toLocaleString(); // '2015/6/24 下午7:49:22',本地時(shí)間(北京時(shí)區(qū)+8:00),顯示的字符串與操作系統(tǒng)設(shè)定的格式有關(guān)
d.toUTCString(); // 'Wed, 24 Jun 2015 11:49:22 GMT',UTC時(shí)間,與本地時(shí)間相差8小時(shí)

獲取當(dāng)前是時(shí)間戳

if (Date.now) {
    alert(Date.now()); // 老版本IE沒(méi)有now()方法
} else {
    alert(new Date().getTime());
}  

練習(xí)

'use strict';
var today = new Date();
if (today.getMonth() === 3 && today.getDate() === 19) {
    alert('親愛(ài)的,我預(yù)定了晚餐,晚上6點(diǎn)在餐廳見(jiàn)!');
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容