19

I have an input on my webpage that I am able to set the date on by getting an ISO string and pulling out the first 10 characters.

date = new Date(); dateInput.value = date.toISOString().substr(0,10); 

This works perfectly. My problem is that when I try to get the date back out. I am getting the date one day off.

var newDate = new Date(dateInput.value); 

I have also tried the following code to make up for it, but it is not always correct either

new Date(Date.parse(element.value) + 86400000)

So my question is: Is there an elegant way to get the correct date consistently. I have been looking around for a little while, but it seems there is not a lot of consistency with date parsing in Javascript.

3
  • Where is the value of date coming from? Commented May 29, 2013 at 23:06
  • think you will find good use for momentjs.com Commented May 29, 2013 at 23:06
  • I have figured out a way to use the string it gives me by just never converting it into a Date, but I am still baffled as to why it is behaving like this. Commented May 30, 2013 at 14:46

6 Answers 6

28

If it's an actual date input on a supporting browser, then it will have a valueAsDate property. There's no need to parse it.

Sign up to request clarification or add additional context in comments.

6 Comments

That's cool. I did not know about that property, but it is giving me the same problem. The date is one day behind what I put in the box. I wonder if it is my browser. I am using Chrome 27.0.1453.94 m. I tried it on Internet Explorer, and it does not support the property (surprise).
Firefox does not support it at all. Too bad. I only need it to work on Web-kit based browsers anyway. (iPhone and Android).
@Duckbrain Are you sure the date is one day behind? What timezone are you in?
Actually there is a need to parse it. HTML5 input type="date" elements revert to standard text input elements in old browsers. This is to maintain compatibility. Therefore, rather than using the valueAsDate property, it would be better to parse the value of the element to allow users to input the date manually (formatted according to a label hidden with modernizr/@media queries).
@CodeO'Clock "If it's an actual date input on a supporting browser"
|
10
var newDate = new Date(dateInput.value + 'T00:00'); 

This will give the correct date in any timezone.

1 Comment

Works perfectly. This answer should be the accepted answer since.
8

It's interpreting the date as UTC, which, for most time zones, will make it seem like "yesterday". One solution could be to add the time-zone offset back into the date to "convert" it to your local timezone.

2 Comments

You are absolutely right. I am no longer working on this project, but this is the correct answer to the question.
@Duckbrain That's why I asked you what timezone you were in...
3

You can also convert the input string from YYYY-MM-DD to MM/DD/YYYY and it then parse the date to get the correct answer.

EXAMPLE:

Don't use:

new Date(Date.parse('2018-09-28')) // Thu Sep 27 2018 19:00:00 GMT-0500 (Central Daylight Time) 

Rather use

new Date(Date.parse('09/28/2018')) // Fri Sep 28 2018 00:00:00 GMT-0500 (Central Daylight Time) 

(NOTE: I am in CDT)

Tested in Chrome, Firefox, Safari, old Edge, and IE.

Comments

1

as said by Marty the problem is the difference between the representation of the timezone of the input (UTC defined by W3C) and the JS timezone (local). The solution is to getTimezoneOffset (which is in minutes) and convert everything to milliseconds:

var today = document.getElementById("myInputDate").valueAsDate; var tomorrow = new Date(today.valueOf() + 86400000 + (today.getTimezoneOffset() * 60000)); 

86400000 = milliseconds of a day

today.getTimezoneOffset() * 60000 = timezoneOffset in milliseconds

Comments

1

Try using .toLocaleString to get the date into the input, and add the time when parsing it, like this:

date = new Date(); dateInput.value = date.toLocaleString("sv-SE", { hour: "2-digit", minute: "2-digit", second: "2-digit" }); 

And to get the value into a Date object use:

new Date(inputdateInput.value + "T00:00"); 

I wrote a little post about converting between Date objects and date inputs here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.