JS格式化日期方法

//格式化日期方法
function FormatDate(dateStr, type) {
    if (dateStr == undefined || dateStr == "") {
        return "";
    }
    var date = new Date(dateStr);
    var year = "" + date.getFullYear() + "";
    var month = "" + (date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : (date.getMonth() + 1) + "";
    var day = "" + (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate()) + "";
    var hour = "" + (date.getHours() < 10 ? ("0" + date.getHours()) : date.getHours()) + "";
    var minute = "" + (date.getMinutes() < 10 ? ("0" + date.getMinutes()) : date.getMinutes()) + "";
    var second = "" + (date.getSeconds() < 10 ? ("0" + date.getSeconds()) : date.getSeconds()) + "";
    var returnDate;
    if (type == "-") {
        //2019-05-01 00:00:00
        returnDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
    }
    else if (type == "/") {
        //2019/05/01 00:00:00
        returnDate = year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second;
    }
    return returnDate;
}

 

你可能感兴趣的:(Html,Js)