date 物件

隨便啦_發表於2019-02-28

建立Date物件

    var now = new Date()
    console.log(typeof now)
    console.log(now) // 代表的是 當前時間物件
    // Tue Feb 26 2019 16:47:03 GMT+0800 (中國標準時間)
複製程式碼

獲取毫秒數

  • Date.now()
  • 時間物件.getTime()
    // 當前時間 距離 1970年1月1日 00:00:00 的毫秒數
    console.log(Date.now())

    var n = new Date()
    console.log(n.getTime())
複製程式碼

格式化時間

  • 將毫秒數 格式化成 時間物件
   var dd = new Date(1550171443214)
   console.log(dd) // Fri Feb 15 2019 03:10:43 GMT+0800 (中國標準時間)
   console.log(typeof dd)
複製程式碼
  • 操作方法
    var now = new Date()
複製程式碼

獲取日期

    console.log('幾號', now.getDate())
複製程式碼

獲取星期 0 ~ 6 (0代表的是週日)

    console.log('星期', now.getDay())
複製程式碼

獲取月份 0 ~ 11 (0代表1月份 11代表12月份)

    console.log('月份', now.getMonth())
複製程式碼

獲取年份

    console.log('年份', now.getFullYear())
複製程式碼

獲取小時

    console.log('小時', now.getHours())
複製程式碼

獲取分鐘

    console.log('分鐘', now.getMinutes())
複製程式碼

獲取秒

    console.log('秒', now.getSeconds())
複製程式碼

獲取毫秒

    console.log('毫秒', now.getMilliseconds())
複製程式碼

獲取毫秒數 距離 1970

    console.log(now.getTime())
複製程式碼

將date物件轉換為字串

    console.log(now.toString());
複製程式碼

把Date物件的時間部分轉換為字串

    console.log(now.toTimeString())
複製程式碼

把 Date 物件的日期部分轉換為字串。

    console.log(now.toDateString())
複製程式碼

根據本地時間格式,把 Date 物件轉換為字串。

    console.log(now.toLocaleString())
複製程式碼

轉換成GMT(世界時)格式字串

    console.log(now.toGMTString())
複製程式碼

相關文章