1. 首页
  2. IT资讯

JS时间日期Date函数方法总结

1.new Date() — 获取当前时间

let now = new Date()

2.setHours与setDate等set方法设置时间

    let now = new Date()    
    now.setHours(10)

3.getTime() 获取时间戳

console.log(now.getTime()); //1589942787285

4.两个时间相减得到的是毫秒数

let now = new Date()
let now1 = new Date('2020-05-19 18:10:25')
console.log(now - now1); //59762285

5.打卡时用到的节流方法

    let endTime = new Date()
    let i = 0
    function sayHi(){
        i++
        if (new Date() - endTime <1000) return;
        console.log('hi',i);
        endTime = new Date()         
    }
    setInterval(() => {
      sayHi()  
    }, 500);

6.获取年月日时分秒星期

let yy = now.getFullYear()
let mm = now.getMonth()+1
let dd = now.getDate()
let hh = now.getHours()
let mi = now.getMinutes()
let sc = now.getSeconds()
let ww = '星期'+'日一二三四五六'[now.getDay()]

本文来自投稿,不代表程序员编程网立场,如若转载,请注明出处:http://www.cxybcw.com/197812.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code