第五章 Date類型

Date類型

創(chuàng)建日期對象

let date = new Date();
console.log(date);
// Thu Jan 31 2019 16:11:59 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
// 不傳參表示顯示當(dāng)前時(shí)間
console.log(new Date('12/12/2012'));
// Wed Aug 08 2018 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
let date2 = new Date(1533657600000);
console.log(date2);
// Wed Aug 08 2018 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)
let date3 = new Date(2018, 8, 8, 17, 22, 45, 15);
console.log(date3);
//Sat Sep 08 2018 17:22:45 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間

毫秒

Date.parse('2018/08/08');
// 1533657600000

通用方法

也擁有toString,toLocaleString,valueof三種方法;

let date = new Date(2018, 8, 8, 17, 22, 45, 15);
console.log(date);
console.log('toString:' + date.toString());
console.log('toLocaleString:' + date.toLocaleString());
console.log('valueOf:' + date.valueOf());
Mac

日期格式化方法

let date = new Date();
console.log('toDateString' + date.toDateString());
console.log('toTimeString:' + date.toTimeString());
console.log('toLocaleDateString:' + date.toLocaleDateString());
console.log('toLocaleTimeString:' + date.toLocaleTimeString());
console.log('toUTCString:' + date.toUTCString());
Mac

組件方法

let date = new Date();
console.log(date);
console.log('getTime: ' + date.getTime());  //毫秒
console.log('getFullYear: ' + date.getFullYear());  //年
console.log('getMonth: ' + date.getMonth());    //月
console.log('getDate: ' + date.getDate());  //日
console.log('getDay: ' + date.getDay());    //星期
console.log('getHours: ' + date.getHours());    //時(shí)
console.log('getMinutes: ' + date.getMinutes());    //分
console.log('getSeconds: ' + date.getSeconds());    //秒
console.log('getTimezoneOffset:' + date.getTimezoneOffset()); //與utc相差時(shí)間

以上方法除getTimezoneOffset外,都有set方法,UTC方法。

Mac
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • 目錄 簡述 獲取毫秒的幾種轉(zhuǎn)換方法 常用的幾種方法 簡述 要?jiǎng)?chuàng)建一個(gè)日期對象,使用new操作符和Date構(gòu)造函數(shù)即...
    A鄭家慶閱讀 223評論 0 0
  • ??引用類型的值(對象)是引用類型的一個(gè)實(shí)例。 ??在 ECMAscript 中,引用類型是一種數(shù)據(jù)結(jié)構(gòu),用于將數(shù)...
    霜天曉閱讀 1,220評論 0 1
  • 1 Object 對象 教程:https://wangdoc.com/javascript/stdlib/obje...
    智勇雙全的小六閱讀 2,507評論 0 0
  • 第5章 引用類型(返回首頁) 本章內(nèi)容 使用對象 創(chuàng)建并操作數(shù)組 理解基本的JavaScript類型 使用基本類型...
    大學(xué)一百閱讀 3,686評論 0 4
  • 本章內(nèi)容 使用對象 創(chuàng)建并操作數(shù)組 理解基本的 JavaScript 類型 使用基本類型和基本包裝類型 引用類型的...
    悶油瓶小張閱讀 784評論 0 0

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