views:

416

answers:

1

Hello,

I've been trying to debug a script of mine and I can't get my head around what's wrong with this:

var date = new Date("19871104071535".replace(
    /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
    '$4:$5:$6 $2/$3/$1'
));
alert(date);

It works in Firefox, IE, Chrome but Safari gives me an "Invalid Date" error. Any ideas?

+1  A: 

The Time and Date are in the wrong order (for just Safari I guess :):

I tested this in Safari and it works (I just swapped Date and Time position in the final string):

var date = new Date("19871104071535".replace(
    /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
    '$2/$3/$1 $4:$5:$6'
));
alert(date);

It will also work in the other browsers because this is what is expected.

Doug Neiner
Thank you, works wonderfully.
James James