1. Home
  2. Docs
  3. node.js
  4. 日期时间

日期时间

获取当前时间

console.log(Date.now());//得到的是当前时间的时间戳1532163468337
console.log(process.uptime());//4.32
console.log(new Date().getTime());//得到的是当前时间的时间戳1532163468337
console.log(new Date());//2018-07-21T08:57:48.337Z
console.log(process.hrtime());//[ 198411, 435296977 ]

JS 时间格式化(时间戳 转 yyyy-MM-dd HH:mm:ss)

formatDate(time) {
    let date = new Date(time);
    let YY = date.getFullYear();
    let MM = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
    let DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    let hh = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
    let mm = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    let ss = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

    // 这里修改返回时间的格式
    return YY + "-" + MM + "-" + DD + " " + hh + ":" + mm + ":" + ss;
}

js判断时间戳是否为当天时间

var str = 1478508411000;
console.log(new Date(str).toDateString());
if (new Date(str).toDateString() === new Date().toDateString()) {
    //今天
    console.log("当天");
} else if (new Date(str) < new Date()){
    //之前
    console.log(new Date(str).toISOString().slice(0,10));
}

函数形式
function isToday(str) {
    if (new Date(str).toDateString() === new Date().toDateString()) {
        //今天
        console.log("当天");
    } else if (new Date(str) < new Date()){
        //之前
        console.log("以前的日期");
    }
}
var str = 1474172800000;
isToday(str);

获取当天的时间戳(Y-M-D)

Date.parse(new Date().toLocaleDateString())
Was this article helpful to you? Yes No

How can we help?