Javascript yyyy-mm-dd converting to incorrect date (mm/dd/yyyy)

294
December 15, 2017, at 10:37 AM

Here is the code where the user enters a date: (it has to be a date picker on my end, but the form has to submit the date field as text – don’t ask)

On submit, I call validation logic in javascript. I’ve attached a screenshot of what it looks like when I try to enter 01/01/2001 as the users birthday. It looks like when I’m converting the value string to a Date object, it’s converting to the wrong date and time. If it would just convert correctly, I could adjust the month and day and year and build a string to send in my second object.

Attaching the picture…

I’ve messed around with UTC and timezones, but to no avail.

I need my output to be a text string "01/01/2001" which I can build as long as I have the correct date going in..but it seems to calculate wrong no matter what I try.

Answer 1

When you construct the Date it is assumed that the string represents a time in UTC since no timezone was provided. The string is parsed as UTC, but the Date object uses your browser's local timezone.

One way to fix that is to use getUTCDay instead of getDay. Same applies to month and year.

Rent Charter Buses Company
READ ALSO
How do I change the size of an input field dynamically based on the size of the placeholder

How do I change the size of an input field dynamically based on the size of the placeholder

I am generating a string that will serve as a URI for an API dynamically, and based on user input for URL Parameters etc

234
How do you url indexOf with multiple options?

How do you url indexOf with multiple options?

I am trying to say that if a URL ends in X, then I want a div to be injected onto the siteHowever, there are multiple X's

262
What is the best practice for filtering tables in web applications, AJAX or JavaScript? [on hold]

What is the best practice for filtering tables in web applications, AJAX or JavaScript? [on hold]

I need to filter a table that has many registers on database (3,000,000+) and to filter it, I need one filter working with words so I have to use "like" queriesBut this seems to be slow when typing because it's making ajax request for every letter introduced...

211