Loading

JS 时间格式化

1、yyyyMMddHHmmss转yyyy-MM-dd HH:mm:ss格式

//时间yyyyMMddHHmmss转yyyy-MM-dd HH:mm:ss格式
//参数示例:'20230518000802'
function dateFormat(dateString) {
    if (dateString != '' && dateString != null && dateString != undefined) {
        var pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
        var formatedDate = dateString.replace(pattern, '$1-$2-$3 $4:$5:$6');
        return formatedDate;
    } else {
        return '';
    }
}
//时间yyyyMMddHHmmss转yyyy-MM-dd HH:mm:ss格式
//参数示例:'20230518000802'
function dateFormat(dateString) {
    if (dateString != '' && dateString != null && dateString != undefined) {
        var formatedDate = dateString.replace(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/,'$1-$2-$3 $4:$5:$6');
        return dateString;
    }
    else{
        return '';
    }
}
//时间yyyyMMddHHmmss转yyyy-MM-dd HH:mm:ss格式
//参数示例:'20230518000802'
function dateFormat(dateString) {
    var formatedDate ='';
    if (dateString != '' && dateString != null && dateString != undefined) {
        if(dateString.length==14){
            formatedDate =`${dateString.substring(0, 4)}-${dateString.substring(4, 6)}-${dateString.substring(6, 8)} ${dateString.substring(8,10)}:${dateString.substring(10,12)}:${dateString.substring(12,14)}`
        } else if(dateString.length==8){
            formatedDate =`${dateString.substring(0, 4)}-${dateString.substring(4, 6)}-${dateString.substring(6, 8)}`;
        } else if(dateString.length==6){
            formatedDate =`${dateString.substring(0, 4)}-${dateString.substring(4, 6)}`;
        }
    }
    else{
        return '';
    }
}

 

2、转换为yyyy-MM-dd hh:mm:ss格式

function repair (i) {
    if (i >= 0 && i <= 9) {
        return "0" + i;
    } else {
        return i;
    }
};
function formatDate(time){
    var date = new Date(time);
    var year = date.getFullYear(),
        month = repair(date.getMonth() + 1),//月份是从0开始的
        day = repair(date.getDate()),
        hour = repair(date.getHours()),
        min = repair(date.getMinutes()),
        sec = repair(date.getSeconds());
    var formatedDate =`${year}-${month}-${day} ${hour}:${min}:${sec}`
    return formatedDate ;            
}

  

3、自定义转换格式

function repair (i) {
    if (i >= 0 && i <= 9) {
        return "0" + i;
    } else {
        return i;
    }
};
//date参数参考:'yyyy-MM-dd HH:mm:ss','new Date().getTime()'
//format默认:'yyyy-MM-dd HH:mm:ss',参考写法'yyyy年MM月-dd日 hh:mm:ss'
function formatDateTime(date, format = 'yyyy-MM-dd hh:mm:ss') {
    date = new Date(date);
    var year = date.getFullYear(),
        month = page.repair(date.getMonth() + 1),//月份是从0开始的
        day = page.repair(date.getDate()),
        hour = page.repair(date.getHours()),
        min = page.repair(date.getMinutes()),
        seconds = page.repair(date.getSeconds()),
        milliseconds = page.repair(date.getMilliseconds()),
        quarter = Math.floor((date.getMonth() + 3) / 3), // 季度
        a = date.getHours() < 12 ? '上午' : '下午', // 上午/下午
        A = date.getHours() < 12 ? 'AM' : 'PM'; // AM/PM
    var formatedDate = format.replace(/yyyy/g, year)
                             .replace(/MM/g, month)
                             .replace(/dd/g, day)
                             .replace(/hh/g, hour)
                             .replace(/mm/g, min)
                             .replace(/ss/g, seconds);
    console.log(formatedDate);
    return formatedDate;
}

 

posted @ 2023-05-18 13:56  流纹  阅读(4075)  评论(0编辑  收藏  举报