Html 如何为输入设置默认值[type="date"]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14212527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:48:46  来源:igfitidea点击:

How to set default value to the input[type="date"]

htmldatehtml-input

提问by hh54188

I have tried (JSFiddle):

我试过(JSFiddle):

<input type="date" value="2012-3-23">

but it doesn't work, how can I set the default value?

但它不起作用,我该如何设置默认值?

回答by Lewis Norton

The date should take the format YYYY-MM-DD. Single digit days and months should be padded with a 0. January is 01.

日期应采用格式YYYY-MM-DD。单位数的天和月应该用 0 填充。一月是 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.

表示日期的字符串。

值:[RFC 3339] 中定义的有效完整日期,附加条件是年份组件是四位或更多位数字,表示大于 0 的数字。

Your code should be altered to:

您的代码应更改为:

<input type="date" value="2013-01-08">

Example jsfiddle

示例 jsfiddle

回答by Taufiq Ahmed

<input type="date" id="myDate" />

<input type="date" id="myDate" />

Then in js :

然后在 js 中:

_today: function () {
  var myDate = document.querySelector(myDate);
  var today = new Date();
  myDate.value = today.toISOString().substr(0, 10);
},

回答by Umair Khalid

A possible solution:

一个可能的解决方案:

document.getElementById("yourDatePicker").valueAsDate = new Date();

Using Moment.js:

使用 Moment.js:

var today = moment().format('YYYY-MM-DD');
document.getElementById("datePicker").value = today;

回答by Toni Tegar Sahidi

if you are using PHP, you can set the value like this

如果您使用的是 PHP,则可以像这样设置值

<input type="date" value="<?php echo date("Y-m-d");?>">

but remember that it would return the date of the server. if you want to use from the client, use javascript instead. hope it helps.

但请记住,它会返回服务器的日期。如果您想从客户端使用,请改用 javascript。希望能帮助到你。

回答by Corioquix

You can use this js code:

你可以使用这个js代码:

<input type="date" id="dateDefault" />

JS

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");

回答by daronwolff

You can do something like this:

你可以这样做:

<input type="date" value="<?php echo date("Y-m-d");?>" name="inicio">

回答by Awais Jameel

The easiest way for setting the current date is.

设置当前日期的最简单方法是。

<input name="date" type="date" value="<?php echo date('Y-m-j'); ?>" required>

<input name="date" type="date" value="<?php echo date('Y-m-j'); ?>" required>

回答by AAYUSH SAINI

$date=date("Y-m-d");
echo"$date";
echo"<br>SELECT DATE: <input type='date'  name='date'  id='datepicker' 
value='$date' required >";

回答by Sagar Khole

// html code

// html代码

<input id="idFdate" type="date" />

// javascript code on main load function

// 主要加载函数上的 javascript 代码

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+"";
}

回答by CharliePrm88

JS Code:

JS代码:

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:

html代码:

<div id="today" > </div>

A little bit rough but it works!

有点粗糙,但它的工作原理!