时间格式化函数

timeFormat(time) {
    console.log("timeFormat:"); //2021-09-03 01:01:01
    if (time != undefined && time.length == 19) {
        time = time.replace(/-/g, "/")
    }
    var date = time == undefined ? new Date() : new Date(time);
    var seperator1 = "-";
    var year = parseInt(date.getFullYear());
    var month = parseInt(date.getMonth()) + 1;
    var strDate = parseInt(date.getDate());
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    console.log("time:" + time);
    console.log("date:" + date);
    console.log("year:" + year);
    return currentdate;
    // HH:mm:ss
    var HH = date.getHours();
    var mm = date.getMinutes();
    var ss = date.getSeconds();
    HH = (HH <= 9) ? ("0" + HH) : HH
    mm = (mm <= 9) ? ("0" + mm) : mm
    ss = (ss <= 9) ? ("0" + ss) : ss
    var seperator2 = ":";
    currentdate += ' ' + HH + seperator2 + mm + seperator2 + ss;
    return currentdate;
    //  return strDate;
},

你可能感兴趣的:(前端javascript时间)