How to set input type date's default value to today?
Given an input element:
<input type="date" />
Is there any way to set the 开发者_StackOverflowdefault value of the date field to today's date?
Like any HTML input field, the browser will leave the date element empty unless a default value is specified within the value
attribute. Unfortunately, HTML5 doesn't provide a way of specifying 'today'
in the HTMLInputElement.prototype.value
.
One must instead explicitly provide a RFC3339 formatted date (YYYY-MM-DD
). For example:
element.value = "2011-09-29"
Use HTMLInputElement.prototype.valueAsDate
:
document.getElementById('datePicker').valueAsDate = new Date();
The JavaScript Date object provides enough built-in support for the required format to avoid doing it manually:
Add this for correct timezone support:
Date.prototype.toDateInputValue = (function() {
var local = new Date(this);
local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
return local.toJSON().slice(0,10);
});
jQuery:
$(document).ready( function() {
$('#datePicker').val(new Date().toDateInputValue());
});
Pure JS:
document.getElementById('datePicker').value = new Date().toDateInputValue();
This relies upon PHP:
<input type="date" value="<?php echo date('Y-m-d'); ?>" />
You could fill the default value through JavaScript as seen here:
http://jsfiddle.net/7LXPq/
$(document).ready( function() {
var now = new Date();
var month = (now.getMonth() + 1);
var day = now.getDate();
if (month < 10)
month = "0" + month;
if (day < 10)
day = "0" + day;
var today = now.getFullYear() + '-' + month + '-' + day;
$('#datePicker').val(today);
});
I would probably put a bit of extra time to see if the month and date are single digits and prefix them with the extra zero...but this should give you an idea.
EDIT: Added check for the extra zero.
Follow the standard Y-m-d format, if you are using PHP
<input type="date" value="<?php echo date("Y-m-d"); ?>">
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 +"T00:00";
$("#theDate").attr("value", today);
});
demo
If you don't want to use jQuery you can do something like this
JS
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;
document.getElementById("theDate").value = today;
demo
TS
const date = new Date()
const year = date.getFullYear()
let month: number | string = date.getMonth() + 1
let day: number | string = date.getDate()
if (month < 10) month = '0' + month
if (day < 10) day = '0' + day
const today = `${year}-${month}-${day}`
document.getElementById("theDate").value = today;
In HTML5 as such, there is no way to set the default value of the date field to today’s date? As shown in other answers, the value can be set using JavaScript, and this is usually the best approach if you wish to set the default according to what is current date to the user when the page is loaded.
HTML5 defines the valueAsDate
property for input type=date
elements, and using it, you could set the initial value directly from an object created e.g. by new Date()
. However, e.g. IE 10 does not know that property. (It also lacks genuine support to input type=date
, but that’s a different issue.)
So in practice you need to set the value
property, and it must be in ISO 8601 conformant notation. Nowadays this can be done rather easily, since we can expect currenty used browsers to support the toISOString
method:
<input type=date id=e>
<script>
document.getElementById('e').value = new Date().toISOString().substring(0, 10);
</script>
If you're doing anything related to date and time in the brower, you want to use Moment.js:
moment().format('YYYY-MM-DD');
moment()
returns an object representing the current date and time. You then call its .format()
method to get a string representation according to the specified format. In this case, YYYY-MM-DD
.
Full example:
<input id="today" type="date">
<script>
document.getElementById('today').value = moment().format('YYYY-MM-DD');
</script>
HTML:
<input type="date" value="2022-01-31">
PHP:
<input type="date" value="<?= date('Y-m-d') ?>">
Date format must be "yyyy-mm-dd"
Javascript
document.getElementById('date-field').value = new Date().toISOString().slice(0, 10);
Jquery
$('#date-field').val(new Date().toISOString().slice(0, 10));
Another Option
If you want to customize the date, month and year just do sum or sub as your wish
精彩评论