0

I have a standard date in ISO format: 1950-01-01 (date of birth)

And I need to convert it to a javascript object, so I can convert it to US Format (01/01/1050).

However when I convert it, it changes it to: Sat Dec 31 1949 17:00:00 GMT-0700

I just need it converted, without any offsets, or changes. If they were born on x day, it is x day.

Here is what I am doing currently:

$("#dob1").val( new Date(client.dob1).toLocaleDateString('en', { day: '2-digit', month: '2-digit', year: 'numeric' }) )

client.dob1 = "1950-01-01"

Final working result, in case anyone stumbles upon this:

$("#dob1").val( new Date(client.dob1).toLocaleDateString('en', { day: '2-digit', month: '2-digit', year: 'numeric', timeZone: "UTC" }) )
triunenature
  • 651
  • 2
  • 7
  • 23
  • 1
    Looks like it is parsed in UTC, while the default string output is in your local timezone. Just use the UTC methods when formatting to US and you'll get 01.01.1950 – Bergi Oct 12 '15 at 22:09
  • 1
    Possible duplicate of [javascript Date timezone issue](http://stackoverflow.com/questions/29174810/javascript-date-timezone-issue) – Matt Johnson-Pint Oct 12 '15 at 22:10
  • @Bergi - Unfortunately, that is an implementation detail that has changed between ES5 & ES6. Best not to rely on it, or get caught when new standards start being rolled out. – Matt Johnson-Pint Oct 12 '15 at 22:14
  • @MattJohnson: Yeah, sure, one should also ensure that the date is parsed in UTC (using whatever means) – Bergi Oct 12 '15 at 22:17

2 Answers2

0

You can simple create a Date object like this.

new Date('2015-10-13')

You can read here more about Date

Dmytro Pastovenskyi
  • 5,240
  • 5
  • 37
  • 56
  • Right, but when I do that, it changes the date back 7 hours, making the date always wrong. SInce we are talking about a specific date, not a datetime. – triunenature Oct 12 '15 at 22:14
  • Try using / instead of - See here: http://stackoverflow.com/questions/29174810/javascript-date-timezone-issue – Josiah Krutz Oct 12 '15 at 22:14
  • @triunenature: it's not back 7 hours. It's still 01. 01. 1950 00:00 **UTC**. Only your output is in a different timezone. – Bergi Oct 12 '15 at 22:16
  • it's because of your local time zone. – Dmytro Pastovenskyi Oct 12 '15 at 22:17
  • @FievelYaltsen, That worked, make that an answer and I'll accept it. It's frusterating that I can't just tell js to give me the date I supplied it. – triunenature Oct 12 '15 at 22:19
  • @Bergi, I understand that it is correct as the object. But I need the *output* to be correct. It's like saying, well the system is right, but it isn't showing right. Doesn't actually help me right? I dont care about the parser, I just need it to give me the date I used. – triunenature Oct 12 '15 at 22:21
  • @triunenature: Just pass `timeZone: "UTC"` to the [options](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString) then. – Bergi Oct 12 '15 at 22:36
  • @Bergi, Thanks so much! That's the part I was missing. It's actually a sane answer. Clearly I need to read the docs more. – triunenature Oct 13 '15 at 01:40
0

You can also replace the dashes with slashes, and make a new Date() from the resulting string.

(some code from https://stackoverflow.com/a/29185654/2033574)

// Manually
date1 = new Date("1950/01/01")

// Or programmatically:
dashDate = "1950-01-01"
date2 = new Date(dashDate.replace(/-/g, '/'))

// Same output
document.write(date1 + "<br>" + date2)
Community
  • 1
  • 1
Josiah Krutz
  • 957
  • 6
  • 16
  • Im marking this as correct, but I prefer Bergi's answer of: toLocaleDateString('en', { day: '2-digit', month: '2-digit', year: 'numeric', timeZone: "UTC" }) – triunenature Oct 13 '15 at 01:54