-1

I have a date like "2015-05-01 09:00:00" in chrome & opera returns the format which i need "Fri May 01 2015 09:00:00 GMT+0530 (India Standard Time)" using the code var austDay = new Date('2015-05-01 09:00:00'); but in firefox and explorer it returns invalid date

RobG
  • 142,382
  • 31
  • 172
  • 209
aish
  • 623
  • 1
  • 7
  • 11
  • Don't use the Date constructor to parse strings, manually parse it. The format you are using will be treated as UTC in some browsers, local in others and NaN in the rest (PS it *should* be treated as UTC according to the current standard, ECMA-262 ed 5). – RobG Jan 25 '15 at 06:03
  • Check the answer: http://stackoverflow.com/questions/15109894/new-date-works-differently-in-chrome-and-firefox – cuongle Jan 25 '15 at 06:06
  • @CuongLe The given answer doesn't tell the user how to parse the date consistently, just explains why... – Farid Nouri Neshat Jan 25 '15 at 12:36

1 Answers1

0

If you want consistency, you should manually parse date strings. If you want "2015-05-01 09:00:00" to be treated as UTC, then:

function parseISOUTC(s) {
  var b = s.split(/\D/);
  var d = new Date(Date.UTC(b[0], --b[1], b[2]||1, b[3]||0, b[4]||0, b[5]||0));
  return d && d.getMonth() == b[1]? d : NaN; 
}

Note that if the date values are invalid (e.g. 2015-02-30) then it will return NaN. You can add validation of time values if you want. Missing values are treated per ECMA-262. Note that 2 digit years will be treated as 20th century.

RobG
  • 142,382
  • 31
  • 172
  • 209