November 19, 2015

Date Object Creation Format Differences in JavaScript

Another issue that came up in my multi-week calendar was due to how date parsing in JavaScript yields different date objects depending on the format:

  • new Date('11/18/2015') creates a date object of 11/18/2015 local time zone.
  • new Date('2015-11-18') creates a date object of 11/18/2015 UTC time zone. For example, if it's Eastern Standard Time (-5:00), then it will be 11/17/2015 19:00.

Try it out on your web browser's console. One good thing is Chrome, IE, and Firefox all seems to have the same behavior. Granted, I ought to be specifying date in a more specific format, but it's interesting to see how the same "date" yields different date objects based on the format. For the multi-week calendar fix, I just removed the native date creation (which I probably should've done from the beginning) and used moment.js's multiple date format creation method. Moment.js documentation has a reference to a nice table of JavaScript date parsing behavior. Hmm, maybe it's time to add some tests...

No comments:

Post a Comment