我想用以下格式转换字符串日期“2018年3月25日11:51:46 PM”
转换为24小时区域设置格式,如“2018年5月10日08:51:46”(法语格式)
当我插入此类日期“2018年3月25日11:51:46 PM”时,我得到的是“2018年5月10日08:51:46”
这是我当前的代码:
String.prototype.toDate = function(format) {
var normalized = this.replace(/[^a-zA-Z0-9]/g, '-');
var normalizedFormat = format.toLowerCase().replace(/[^a-zA-Z0-9]/g, '-');
var formatItems = `enter code here`
normalizedFormat.split('-');
var dateItems = normalized.split('-');
var monthIndex = formatItems.indexOf("mm");
var dayIndex = formatItems.indexOf("dd");
var yearIndex = formatItems.indexOf("yyyy");
var hourIndex = formatItems.indexOf("hh");
var minutesIndex = formatItems.indexOf("ii");
var secondsIndex = formatItems.indexOf("ss");
var today = new Date();
var year = yearIndex > -1 ? dateItems[yearIndex] : today.getFullYear();
var month = monthIndex > -1 ? dateItems[monthIndex] - 1 : today.getMonth() - 1;
var day = dayIndex > -1 ? dateItems[dayIndex] : today.getDate();
var hour = hourIndex > -1 ? dateItems[hourIndex] : today.getHours();
var minute = minutesIndex > -1 ? dateItems[minutesIndex] : today.getMinutes();
var second = secondsIndex > -1 ? dateItems[secondsIndex] : today.getSeconds();
if ((this.split(" ")[2] == "PM") && hour < 12) {
hour = hour + 12;
} else if ((this.split(" ")[2] == "AM") && hour == 12) {
hour = hour - 12;
}
console.log(year + " " + month + " " + day + " " + hour + " " + minute + " " + second);
return new Date(year, month, day, hour, minute, second);
};
logd = "3/25/2018 11:51:46 PM".toDate("mm/dd/yyyy hh:ii:ss");
console.log(logd.toLocaleString());