If I were in C#, this code would have worked fine:
var day1 = new DateTime(2014, 7, 11); // C#
If you answered 30 June 2014, you’re right! Of course, that’s just what you would expect, too. WAT?
Of course, if you use something hideous like this, it works correctly (in the United States, which expects M/D/Y format):
This, too, will give you what you expect, though it’s a bit verbose:
Go ahead and play with these options yourself here:
Reading Dates (in older browsers)
This has been fixed with ECMAScript 5, but older browsers will still misbehave. If you try to read a zero-padded date, such as “09” for September, using parseInt, you may get an unexpected result because parseInt assumes zero-prefixed strings are in octal format. To avoid this potential issue, always pass in the second parameter to parseInt (for which you’ll typically want to pass a 10). Alternately, you can override parseInt so that if it is called with just one argument it will always use 10 for the radix, on all browsers, as shown here. The fiddle below demonstrates the issue – if you’re using a modern browser, the result of the first operation should be 9. Try it on an older browser and see what you get.
Any other date-related tips you’d like to share? I’d love to hear them in the comments.