JS 时间格式化

JS 时间格式化1、yyyyMMddHHmmss转yyyy-MM-ddHH:mm:ss格式//时间yyyyMMddHHmmss转yyyy-MM-ddHH:mm:ss格式//参数示例:'20230518000802'functiondateFormat(dateString){if(

大家好,欢迎来到IT知识分享网。

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;
}

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/30066.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信