JS实现CST格式时间转正常‘yyyy-MM-dd HH:mm:ss’

注:CST转成GMT时间存在new Date()导致原时间会增加14h,所以在设置时间时,需要-14h。

function cstDateFormat(date, format) {
    date = new Date(date);
    date.setHours(date.getHours() - 14);
    var dset = {
        'M+': date.getMonth() + 1, // 月
        'd+': date.getDate(), // 日
        'H+': date.getHours(), // 时
        'm+': date.getMinutes(), // 分
        's+': date.getSeconds(), // 秒
        'q+': Math.floor((date.getMonth() + 3) / 3), // 刻钟
        'S': date.getMilliseconds() // 毫秒
    };

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }

    for (var i in dset) {
        if (new RegExp('(' + i + ')').test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? dset[i] : ('00' + dset[i]).substr(('' + dset[i]).length));
        }
    }
    return format;
}

//方法调用
var timeString = cstDateFormat('Thu Jan 13 09:42:25 CST 2022', 'yyyy-MM-dd HH:mm:ss');

  • 美好的一天,不应该被破坏,加油💪🏻
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容