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

日期时间

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);
Was this article helpful to you? Yes No

How can we help?