I have tried (JSFiddle):
<input type="date" value="2012-3-23">
but it doesn't work, how can I set the default value?
I have tried (JSFiddle):
<input type="date" value="2012-3-23">
but it doesn't work, how can I set the default value?
The date should take the format YYYY-MM-DD
. Single digit days and months should be padded with a 0. January is 01.
From the documentation:
A string representing a date.
Value: A valid full-date as defined in [RFC 3339], with the additional qualification that the year component is four or more digits representing a number greater than 0.
Your code should be altered to:
<input type="date" value="2013-01-08">
if you are using PHP, you can set the value like this
<input type="date" value="<?php echo date("Y-m-d");?>">
but remember that it would return the date of the server. e.g. if your server in USA and your client in Indonesia, it may differ 1 day.
But if you want to use from the client, use javascript solution instead. hope it helps.
You can use this js code:
<input type="date" id="dateDefault" />
JS
function setInputDate(_id){
var _dat = document.querySelector(_id);
var hoy = new Date(),
d = hoy.getDate(),
m = hoy.getMonth()+1,
y = hoy.getFullYear(),
data;
if(d < 10){
d = "0"+d;
};
if(m < 10){
m = "0"+m;
};
data = y+"-"+m+"-"+d;
console.log(data);
_dat.value = data;
};
setInputDate("#dateDefault");
1 - @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
<input type="date" "myDate">
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
var today = new Date();
$('#myDate').val(today.getFullYear() + '-' + ('0' + (today.getMonth() + 1)).slice(-2) + '-' + ('0' + today.getDate()).slice(-2));
2 - @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
<input type="datatime-local" id="myLocalDataTime" step="1">
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
var today = new Date();
$('#myLocalDataTime').val(today.getFullYear() + '-' + ('0' + (today.getMonth() + 1)).slice(-2) + '-' + ('0' + today.getDate()).slice(-2)+'T'+today.getHours()+':'+today.getMinutes());
// html code
<input id="idFdate" type="date" />
// javascript code on main load function
function loadFunction() {
// body...
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){
dd='0'+dd;
}
if(mm<10){
mm='0'+mm;
}
today = yyyy+'-'+mm+'-'+dd;
document.getElementById("idFdate").defaultValue =today+"";
}
JS Code:
function TodayDate(){
let data= new Date();
return data.getFullYear().toString()+'-' + (data.getMonth()+1).toString()+'-' + data.getDate().toString()
}
document.getElementById('today').innerHTML = '<input type="date" name="Data" value="'+TodayDate()+'" ><br>';
Html Code:
<div id="today" > </div>
A little bit rough but it works!
Here are three statements for three different dates in the form with three type=date fields.
$inv_date
is the current date:
$inv_date = date("Y-m-d");
$inv_date_from
is the first day of the current month:
$inv_date_from = date("Y") . "-" . date("m") . "-" . "01";
$inv_date_to
is the last day of the month:
$inv_date_to = date("Y-m-t", strtotime(date("Y-m-t")));
I hope this helps :)