views:

39

answers:

2

Hi folks,
I have next date string:

"Thu Nov 14 0002 01:01:00 GMT+0200 (GTB Standard Time)"

and I'm trying to convert it to the Date object:

date = new Date("Thu Nov 14 0002 01:01:00 GMT+0200 (GTB Standard Time)")  
=> Invalid Date {}

and it doesn't work. And

date = new Date("Thu Nov 14 2 01:01:00 GMT+0200 (GTB Standard Time)")  
=> Invalid Date {}

doesn't work too

but

date = new Date("Thu Nov 14 2002 01:01:00 GMT+0200 (GTB Standard Time)")

works

Does anyone know an elegant way to parse it ?

+1  A: 
Pointy
+2  A: 

You can set any date. including minutes,hours and milliseconds directly using a timestamp- dates before 1970 are negative integers.

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
kennebec
Nice idea with substracting, thank you.
Tumtu