Reputation: 121
I want to format the date value I got from the HTML datepicker
. I used the js class new Date()
, and put the date value got from the HTML datepicker
in it. The result shows that the actual date after the formatting is one day smaller than the date I selected in the datepicker
, can anyone help me solve this issue? The code is as follow.
$("#date").on("click",function(){
var date2 = new Date($("#date").val());
alert(date2.getDate());
Here is the declaration for HTML part
<input type="date" name="date" id="date" max="2013-12-31" min="2013-01-01">
Result
Upvotes: 0
Views: 150
Reputation: 16540
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.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCDate
Example: http://jsbin.com/vojanisiqu/1/edit?html,js,output
Upvotes: 1