[英]converting data format (Feb 01 2021 10:53:00am KST => 2021-02-01 10:53:00am)
I need to filter datas by it's dates.我需要按日期过滤数据。
In order to do that I have to convert date to Number.为此,我必须将日期转换为数字。
but I am getting this NaN error.但我收到了这个 NaN 错误。
How can I convert我怎样才能转换
Feb 01 2021 10:53:00am KST
into进入
2021-02-01 10:53:00
or Is there any way to convert或者有什么方法可以转换
Feb 01 2021 10:53:00am KST
to Number directly?直接编号?
Having a look at Date.parse看看Date.parse
This one parses in Chrome "Feb 01 2021 10:53:00"
这个在 Chrome
"Feb 01 2021 10:53:00"
中解析
const d = new Date("Feb 01 2021 10:53:00") console.log(d)
So let's try more specific所以让我们尝试更具体的
// 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.