0

I simply run:

new_time = new Date("10/02/2017").getTime()

and it return - 1506891600000

But, whet i run:

new_time = new Date("26/02/2017").getTime()
new_time = new Date("25/02/2017").getTime()
new_time = new Date("16/02/2017").getTime()
new_time = new Date("15/02/2017").getTime()

it return - NaN

What's the problem with Date().getTime()?

Ivan Stasiuk
  • 292
  • 6
  • 12
  • 1
    Try `02/15/2017` or `2017/02/15`. Or if you don't have to use values like this `new Date(2017, 02, 15)` would be even more accurate. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – Alex Szabo Feb 26 '17 at 19:00
  • 1
    ^^ just remember if you do that multi-part constructor, the months start with 0 = January. – T.J. Crowder Feb 26 '17 at 19:03
  • In American date, you have month/day/year. And JavaScript is based on it. You can use ISO date too. – Gilsdav Feb 26 '17 at 19:04
  • Thanks for the answer :) – Ivan Stasiuk Feb 26 '17 at 19:05
  • Use an ISO format: `new Date("2017-02-10").getTime()` – alex Feb 26 '17 at 19:07
  • Better duplicate: http://stackoverflow.com/questions/39244286/error-parsing-date-string-with-new-date – T.J. Crowder Feb 26 '17 at 19:08
  • @Gilsdav: Not officially, the `Date` constructor is **not** defined as accepting `mm/dd/yyyy`. I've never met a JavaScript engine that didn't do so (even in non-U.S. locales), but that's different. – T.J. Crowder Feb 26 '17 at 19:09
  • 1
    @alex—that will be parsed by most browsers as UTC, so will produce a local date for 9 Feb where the host is set to a timezone west of Greenwich. – RobG Feb 26 '17 at 20:12

0 Answers0