Format a date in Javascript without converting to local timezone
The only solution I found is replacing -
with /
:
input = "2012-01-01 01:02:03";var text = input.replace('-', '/');var d = new Date(Date.parse(text));hours = d.getHours();
In my case the input was 2013-05-22T16:45:00Z
so I replaced T
and Z
as well:
input = "2013-05-22T16:45:00Z";var text = input.replace(/[TZ]/, '').replace('-', '/');var d = new Date(Date.parse(text));hours = d.getHours();
That way Javascript deals with it without knowing anything about timezone and stop acting smartly.
I think that to do with Dates you will need to use some Date "framework" like Globalize jQuery plugin or Datejs to get cross-browser behavior. Doing this, you will be able to configure a pattern to be used in the date parsing.