我需要按日期过滤数据。
为了做到这一点,我必须将日期转换为数字。
但我收到此 NaN 错误。
如何转换
Feb 01 2021 10:53:00am KST
进入
2021-02-01 10:53:00
或者有什么方法可以转换
Feb 01 2021 10:53:00am KST
直接编号?
答案 0 :(得分:2)
这个在 Chrome 中解析 "Feb 01 2021 10:53:00"
const d = new Date("Feb 01 2021 10:53:00")
console.log(d)
所以让我们尝试更具体的
// We want YYYY-MO-DDTHH:MM:SS.MSC+hh:mm
const months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
const pad = num => ("0"+num).slice(-2);
const dString = "Feb 01 2021 10:53:00am KST"; // assuming mmm dd yyyy hh:mm:ssxx KST where xx is am or pm
let [mmm,dd,yyyy,tString,tz] = dString.split(" ");
let [_,hh,mm,ss,ampm] = tString.match(/(\d{2}):(\d{2}):(\d{2})(am|pm)/)
hh = pad(+hh + (ampm==="pm" ? 12 : 0));
const month = pad(months.indexOf(mmm)+1)
const newDString = `${yyyy}-${month}-${dd}T${hh}:${mm}:${ss}.000-09:00`
const date = new Date(newDString)
console.log(newDString,date)