D.Kenny D.Kenny - 1 year ago 84
Javascript Question

How to format HTML datepicker by using js

I want to format the date value I got from the HTML

. I used the js class
new Date()
, and put the date value got from the HTML
in it. The result shows that the actual date after the formatting is one day smaller than the date I selected in the
, can anyone help me solve this issue? The code is as follow.

var date2 = new Date($("#date").val());

Here is the declaration for HTML part

<input type="date" name="date" id="date" max="2013-12-31" min="2013-01-01">


enter image description here

Answer Source

Use getUTCDate() instead of getDate() to ignore UTC offsets.

In your case you're GMT-5, so it is removing 5 hours from the date entered and that puts it into the previous day when using getDate().

toUTCString() will return the full date.


Example: http://jsbin.com/vojanisiqu/1/edit?html,js,output

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download