注:CST轉(zhuǎn)成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;
}
//方法調(diào)用
var timeString = cstDateFormat('Thu Jan 13 09:42:25 CST 2022', 'yyyy-MM-dd HH:mm:ss');
- 美好的一天,不應該被破壞,加油????