new Date() in javascript

It appears that the date object in javascript can have some variances. I had a bug in my code which was only affecting FireFox users on Windows machines. After some debugging time, I figured out that the date object was returning a differently formated value.

These are some examples:
FireFox on Windows 7: Wed Dec 07 2011 16:26:38 GMT-0800 (Pacific Standard Time)
IE 8 on Windows 7: Wed Dec 7 16:29:21 PST 2011
Chrome on Windows 7: Wed Dec 07 2011 16:45:45 GMT -0800 (Pacific Standard Time)
FireFox on OS X Lion: Wed Dec 07 2011 16:37:12 GMT-0800 (PST)
Opera on OS X Lion: Wed Dec 07 2011 16:37:21 GMT-0800
Safari on OS X Lion: Wed Dec 07 2011 16:44:54 GMT-0800 (PST)

It appears that the format of this is left up to the browser to decide. It would be nice if they all followed a specific format. Well it was because of the (Pacific Standard Time) not being recognized by php’s strtotime, that my code failed to work. Some helpful members on IRC brought it to my knowledge that it is up to the browser really.

I find it interesting that out of all the tests, it appears IE just takes a completely different method. While the other browsers are at least semi-similar other than the time zone string at the end. OS X at least appears to be consistent and it appears Opera may just be stripping the string parenthesis from the end.

At least there is a simple solution. Just use some regular expressions to strip off (Pacific Standard Time) and any other variation.

	$date = preg_replace('~\(([^(]+)\)~', '', $_POST['date']);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.