Bit*_*rse 3 javascript string parsing date
我有下一个日期字符串:
"Thu Nov 14 0002 01:01:00 GMT + 0200(GTB标准时间)"
我正在尝试将其转换为Date对象:
date = new Date("Thu Nov 14 0002 01:01:00 GMT+0200 (GTB Standard Time)")
=> Invalid Date {}
Run Code Online (Sandbox Code Playgroud)
它不起作用.和
date = new Date("Thu Nov 14 2 01:01:00 GMT+0200 (GTB Standard Time)")
=> Invalid Date {}
Run Code Online (Sandbox Code Playgroud)
不起作用
但
date = new Date("Thu Nov 14 2002 01:01:00 GMT+0200 (GTB Standard Time)")
Run Code Online (Sandbox Code Playgroud)
作品
有谁知道解析它的优雅方式?
您可以设置任何日期.包括直接使用1970年之前的时间戳日期的分钟,小时和毫秒是负整数.
alert(new Date(-62076675540000).toUTCString());
// >> Wed, 13 Nov 0002 23:01:00 GMT
Or you can set the date as a string by replacing the years to make it over 1000,
then subtracting the amount you added with setFullYear()
var d=new Date("Thu Nov 14 1002 01:01:00 GMT+0200 (GTB Standard Time)")
d.setFullYear(d.getFullYear()-1000)
alert(d.toUTCString())
// >> Wed, 13 Nov 0002 23:01:00 GMT
You can automate a conversion to timestamps-
var s="Thu Nov 14 0002 01:01:00 GMT+0200 (GTB Standard Time)";
var y=s.split(' ')[3], y2=5000+(+y);
var d=new Date(s.replace(y,y2));
d.setFullYear(d.getFullYear()-5000)
var timestamp=+d;
alert(timestamp)
// >> -62076675540000
Run Code Online (Sandbox Code Playgroud)