js获取当前时间 格式“yyyy-MM-dd HH:MM:SS”

js获取当前时间 格式“yyyy-MM-dd HH:MM:SS”

js获取当前时间 格式“yyyy-MM-dd HH:MM:SS”

function getNowFormatDate() {


    var date = new Date();  //获取当前时间
    var month = date.getMonth() + 1; //月份处理
    var dateStr = date.getDate();
    //月份为1-9月,在其前面加0
    if (month >0 && month <= 9) {
        month = "0" + month;
    }
    //日期为1-9号,在其前面加了0,

    if (dateStr >= 0 && dateStr <= 9) {
        dateStr = "0" + dateStr ;
    }
     var s1 = "-";//定义年月日分隔符-
     var s2 = ":";//定义时分秒分隔符:
    //拼接年月日,时分秒
    var currDate = date.getFullYear() + s1 + month + s1 + dateStr
            + " " + date.getHours() + s2 + date.getMinutes()+ s2 + date.getSeconds();
    return currDate;
}

猜你喜欢

转载自blog.csdn.net/qq_40580023/article/details/84527898