JS 时间格式化_js日期格式转换

JS 时间格式化_js日期格式转换1、yyyyMMddHHmmss转yyyy-MM-dd HH:mm:ss格式 //时间yyyyMMddHHmmss转yyyy-MM-dd HH:mm:ss格式 //参数示例:'20230518000802' function dateFormat(dateString) { if (

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

 

今天的文章JS 时间格式化_js日期格式转换分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/50500.html

(0)
编程小号编程小号
上一篇 2023-09-02
下一篇 2023-09-02

相关推荐

发表回复

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