In HTML5 there is not a native way of specifying 'today' in the value attribute. Here is the jQuery code I like very much. How to extend this code to set
- today's date to
var today
- tomorrow's date to
var tomorrow
- any date calculated to
var anydate
(calculated/initiated fromvar today
?)
and define the following 3 id-s accordingly:
#theDate
#theTomorrow
#theAnydate
HTML
<input type="date" id="theDate">
jQuery
$(document).ready(function() {
var date = new Date();
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear();
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
var today = year + "-" + month + "-" + day;
$("#theDate").attr("value", today);
});
In that instance, you could potentially write a script to simply
+1
to find tomorrow's date, but you would first have to add a default value to yourinput id
for today's date.As far as anydate? Not entirely sure what you mean there. Like a datepicker?
The question was a bit unclear, but I figured I'd help as much as I could with the info provided.
To assign a date via jQuery, you could always do something like this...
http://jsfiddle.net/SinisterSystems/4XkVE/4/
HTML:
jQuery:
EDIT:
Furthermore, to find both of today's date, and tomorrow's date, but ensure the end of the month or end of the year won't affect it, use this instead:
http://jsfiddle.net/SinisterSystems/4XkVE/6/
HTML:
jQuery
Use toISOString(), which always returns a "Z" timezone ISO8601 string, and truncate it to just the date.
Building on Nicholas Hazel's response and user113716 answer about leading zeroes for dates, here's a concise function to get a date formmated as YYYY-MM-DD and set the value of a "date" type input control.
http://jsfiddle.net/wloescher/2t8v7fnf/2/
HTML
JavaScript