在 NODEJS 中转换 ' June 09 2021, 01:50:00 PM' 到 2021-06-09 13:50:00 日期格式



如何在NODEJS中将"2021年6月9日下午01:50:00"转换为2021-06-09 13:50:00日期格式。

您可以这样转换:

const convertDateTo24Hour = (date) => {
var elem = date.split(',');
var msplit = date.split(' ');
let months = ["January", "February", "March", "April", "May", "June", "July", 
"August", "September", "October", "November", "December"];
let index = months.indexOf(msplit[0]) + 1;
let newDate = msplit[2].replace(',','') + "-" + index + "-" + msplit[1];
var stSplit = elem[1].split(' ');
var abc = stSplit[1].split(":");
var stHour = abc[0];
var stMin = abc[1];
var stSec = abc[2];
var stAmPm = elem[1].split(" ");
if (stAmPm[2] == 'PM') {
if (stHour != 12) {
stHour = stHour * 1 + 12;
}
} else if (stAmPm[2] == 'AM' && stHour == '12') {
stHour = stHour - 12;
} else {
stHour = stHour;
}
return newDate + " " + stHour + ':' + stMin + ":" + stSec;
}

您可以使用Date构造函数进行解析,然后使用Date.toLocaleString格式化为ISO日期输出,如下所示:

const input = 'June 09 2021, 01:50:00 PM';
const inputFormat = 'MMMM DD YYYY H:mm:ss a';
const d = new Date(input);
console.log('Result:', d.toLocaleString('sv'));

相关内容

  • 没有找到相关文章

最新更新