JS中Date對象

單詞

  • width
  • Date:日期
  • day:日
  • Week:周
  • Month:月
  • Hour:小時
  • Minute:分?jǐn)?shù)
  • Second:秒

Date對象是什么

  • Date對象表示某個時間點(diǎn)。
  • 用途:用于處理編程中與日期和時間有關(guān)的操作。
  • Date對象的內(nèi)部包含一個數(shù)字。(該數(shù)字表示自1970年1月1日0點(diǎn)0分0秒以來經(jīng)過的毫秒數(shù))
  • Date對象使用構(gòu)造函數(shù)Date()創(chuàng)建。

創(chuàng)建Date對象的四種方法。

不傳參

語法:

var d = new Date();

返回值:表示當(dāng)前系統(tǒng)時間

傳入數(shù)字

語法:

var d = new Date(milliseconds);

傳入時間字符串

語法:

var d = new Date(dateString);

傳入7個數(shù)字

語法:

var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

getFullYear()年

定義

getFullYear()方法返回指定日期的年份(1000 年到 9999 年之間的日期的四位數(shù)字)。

語法
Date.getFullYear()

無參數(shù)

返回值

數(shù)值,表示指定日期的年份。

示例
let day = new Date
   let nian = day.getFullYear();
      console.log(nian);  //返回本年

getMonth() 月(從 0-11)。

定義

getMonth()方法根據(jù)本地時間返回指定日期的月份(從 0 到 11)。

  • 注意:一月為 0,二月為 1,依此類推。
語法
Date.getMonth()

無參數(shù)

返回值

數(shù)值,從 0 到 11,表示月份。

示例
let day =new Date
let yue = day.getMonth()+1
      console.log(yue)  // 加一才能對上正確日期

getDate() 日(從 1 到 31)。

定義

getDate 方法返回指定日期在月中的第幾天(從 1 到 31)。

語法

Date.getDate()

無參數(shù)

返回值

數(shù)值,從 1 到 31,表示一個月中的哪一天。

示例
let day = new Date()
      let yue = day.getDate()
      console.log(yue)   //返回月的某天

getHours()返回小時(從 0-23)。

定義

getHours() 方法返回指定日期和時間的小時數(shù)(從 0 到 23)。

語法
Date.getHours()

無參數(shù)

返回值

數(shù)值,從 0 到 23,表示小時。

示例
let day = new Date()
let H = day.getHours()
      console.log(H);  //返回本地小時

getMinutes()分鐘(從 0-59)

定義

getMinutes() 方法返回指定日期和時間的分鐘數(shù)(從 0 到 59)。

語法
Date.getMinutes()

無參數(shù)

返回值

數(shù)值,從 0 到 59,表示分鐘。

示例
let day = new Date();
      let fenzhong = day.getMinutes();
      console.log(fenzhong);  //返回本地分鐘

getSeconds() 秒數(shù)(從 0-59)

定義

getSeconds()方法返回指定日期和時間的秒數(shù)(從 0 到 59)。

語法
Date.getSeconds()

無參數(shù)

返回值

數(shù)值,從 0 到 59,表示秒。

示例
let day = new Date()
let miao = day.getSeconds();
      console.log(miao);    //返回本地秒數(shù)

getMilliseconds()毫秒( 0 到 999)

定義

getMilliseconds() 方法返回指定日期和時間的毫秒數(shù)(從 0 到 999)。

語法
Date.getMilliseconds()

無參數(shù)

返回值

數(shù)值,從 0 到 999,表示毫秒。

let d = new Date();
      let n = d.getMilliseconds();
      console.log(n);   //根據(jù)本地時間返回毫秒

getDay()返回星期幾(0-6)。

定義

getDay() 方法返回指定日期是星期幾(從 0 到 6)。

  • 注意:星期日為 0,星期一為 1,依此類推。
語法
Date.getDay()

無參數(shù)

返回值

數(shù)值,從 0 到 6,表示星期幾。

示例
let day = new Date()
let week = day.getDay()
      console.log(week);  //返回周幾 0表示周天,1表示周一,以此類推

getTime() 返回自 1970/01/01 以來的毫秒數(shù)

定義

getTime() 方法返回從 1970 年 1 月 1 日午夜到指定日期之間的毫秒數(shù)。

語法
Date.getTime()

無參數(shù)

返回值

數(shù)值,表示自 1970 年 1 月 1 日午夜以來的毫秒數(shù)。

示例
var d = new Date();
      var n = d.getTime();
      console.log(n);  //返回自 1970/01/01 以來的毫秒數(shù)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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