2
console.log(new Date('2016-05-24').toISOString()); // '2016-05-24T00:00:00.000Z'
console.log(new Date('05/26/2016').toISOString()); // '2016-05-23T23:00:00.000Z' // why?

I am sending data to the server to parse and want to ensure that server will encode my date correctly.

What is the simplest way to convert date to string as '2016-05-24T00:00:00.000Z' in both cases?

Thanks

Ben
  • 54,723
  • 49
  • 178
  • 224
user1153896
  • 293
  • 7
  • 17
  • 1
    You are aware those are two different dates, right? One is 24 May, one is 26 May. (That's not the source of the error, though.) – Ben Apr 23 '16 at 14:55
  • Possible duplicate of [Why does Date.parse give incorrect results?](http://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – Ben Apr 23 '16 at 15:02
  • that is just typo ... – user1153896 Apr 23 '16 at 15:02
  • The JavaScript `Date` constructor is well known for accepting an absurd number of different formats and providing completely unpredictable results. Don't push it to the limit ;-) – Álvaro González Apr 23 '16 at 15:04

3 Answers3

3
console.log(new Date('2016-05-24 GMT').toISOString()); // '2016-05-24T00:00:00.000Z'
console.log(new Date('05/24/2016 GMT').toISOString()); // '2016-05-24T00:00:00.000Z'

Append the timezone to the date before creating a new date object so that the string parsing code in the Date constructor doesn't get confused. Always disambiguate if possible.

Your code was using different timezones for each parse because of the way the dates were formatted. One was using +0 timezone, other was using -1 timezone hence the date being pulled back an hour when the ISO string was created.

Daniel Wardin
  • 1,840
  • 26
  • 48
  • Probably better than my answer - definitely more clean, but the user may not know the timezone. – Ben Apr 23 '16 at 15:11
1

One is parsing in UTC time, one is parsing in local time.

new Date('2016-05-24').toISOString()  // '2016-05-24T00:00:00.000Z'
new Date('05/24/2016').toISOString()  // '2016-05-24T07:00:00.000Z'

Playing around, here's one solution:

new Date(new Date('05/24/2016') - (new Date()).getTimezoneOffset() * 60000).toISOString() // '2016-05-24T00:00:00.000Z'

The strategy:

  • Create the new offset date
  • Subtract the offset
  • Create a new date from that result

Reference links:


On further consideration, I'd recommend parsing the date string into something that is "universal" before passing it to the date constructor. Something like:

var tmp = ('05/24/2016').split('//');
var universal = [tmp[2], tmp[0], tmp[1]].join('-'); // 2016-05-24
...

Also, Moment.js does this sort of thing very neatly.

Community
  • 1
  • 1
Ben
  • 54,723
  • 49
  • 178
  • 224
  • console.log(new Date(new Date('01/24/2016') - (new Date()).getTimezoneOffset() * 60000).toISOString()) ??. I need universal way to convert string -> date -> toISOFormat – user1153896 Apr 23 '16 at 15:10
  • I recommend parsing the string with slashes first to get it into a format that's "universal" before passing it to the date constructor. – Ben Apr 23 '16 at 15:15
  • I need time to be '00:00:00.000Z' and date 'yyyy-MM-dd' and it should work for all dates. You sample doesn't work for other dates (e.g. console.log(new Date(new Date('01/24/2016') - (new Date()).getTimezoneOffset() * 60000).toISOString()) ---> 2016-01-24T01:00:00.000Z --> what is wrong) – user1153896 Apr 23 '16 at 15:16
  • You're right, it only works to fix the local offset for slash-format dates. So, you say you want the date to be `yyyy-mm-dd` but then in the example you have `mm/dd/yyyy`. You need to decide which one you want, and use that format consistently. Parse one format into the other when necessary. – Ben Apr 23 '16 at 15:20
  • the whole point is to accept different formats and return a consistent result yyyy-MM-ddTHH:mm .... Anyway, thanks for your time – user1153896 Apr 23 '16 at 15:32
0

Use the getDate(), getMonth() and getFullYear() methods to strip out what you need.

Lota
  • 1
  • 2
  • well, it is not what I am looking for. I believe must be better way. Otherwise I would have to add "0" if getDate() < 10 etc ... – user1153896 Apr 23 '16 at 15:01