Javascript 在输入类型日期中设置日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12346381/
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
set date in input type date
提问by Sender
<input id="datePicker" type="date" />?
i will set today date in datepicker input type datein chrome.
我将在 chrome中的 datepicker 输入类型日期中设置今天的日期。
$(document).ready( function() {
var now = new Date();
var today = now.getDate() + '/' + (now.getMonth() + 1) + '/' + now.getFullYear();
alert(today);
$('#datePicker').val(today);
});?
but it's not working
但它不起作用
Edit This jsFiddle- Please try in chrome.
编辑此 jsFiddle- 请在 chrome 中尝试。
回答by Dhanasekar
Fiddle link : http://jsfiddle.net/7LXPq/93/
小提琴链接:http: //jsfiddle.net/7LXPq/93/
Two problems in this:
这有两个问题:
- Date control in HTML 5 accepts in the format of Year - month - day as we use in SQL
- If the month is 9, it needs to be set as 09 not 9 simply. So it applies for day field also.
- HTML 5 中的日期控件接受我们在 SQL 中使用的年 - 月 - 日格式
- 如果月份为9,则需要将其设置为09,而不是简单地设置为9。所以它也适用于日场。
Please follow the fiddle link for demo:
请按照小提琴链接进行演示:
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = now.getFullYear()+"-"+(month)+"-"+(day) ;
$('#datePicker').val(today);
回答by int32_t
document.getElementById("datePicker").valueAsDate= new Date()
document.getElementById("datePicker").valueAsDate= new Date()
should work.
应该管用。
回答by Oliv Utilo
Update: I'm doing this with date.toISOString().substr(0, 10)
. Gives the same result as the accepted answer and has good support.
更新:我正在用date.toISOString().substr(0, 10)
. 给出与接受的答案相同的结果并得到很好的支持。
回答by Gourav Makhija
var today = new Date().toISOString().split('T')[0];
$("#datePicker").val(today);
Above code will work.
上面的代码会起作用。
回答by Umair Khalid
to me the shortest way to solve this problem is to use moment.js and solve this problem in just 2 lines.
对我来说,解决这个问题的最短方法是使用 moment.js 并在短短 2 行中解决这个问题。
var today = moment().format('YYYY-MM-DD');
$('#datePicker').val(today);
回答by Dominic
Your code would have worked if it had been in this format: YYYY-MM-DD
, this is the computer standard for date formats http://en.wikipedia.org/wiki/ISO_8601
如果采用以下格式YYYY-MM-DD
,您的代码会起作用:,这是日期格式的计算机标准http://en.wikipedia.org/wiki/ISO_8601
回答by aashah7
I usually create these two helper functions when using date inputs:
我通常在使用日期输入时创建这两个辅助函数:
// date is expected to be a date object (e.g., new Date())
const dateToInput = date =>
`${date.getFullYear()
}-${('0' + (date.getMonth() + 1)).slice(-2)
}-${('0' + date.getDate()).slice(-2)
}`;
// str is expected in yyyy-mm-dd format (e.g., "2017-03-14")
const inputToDate = str => new Date(str.split('-'));
You can then set the date input value as:
然后,您可以将日期输入值设置为:
$('#datePicker').val(dateToInput(new Date()));
And retrieve the selected value like so
并像这样检索选定的值
const dateVal = inputToDate($('#datePicker').val())
回答by Eda Jede
Datetimepicker always needs input format YYYY-MM-DD, it doesn't care about display format of your model, or about you local system datetime. But the output format of datetime picker is the your wanted (your local system). There is simple example in my post.
Datetimepicker 总是需要输入格式 YYYY-MM-DD,它不关心模型的显示格式,也不关心本地系统日期时间。但是日期时间选择器的输出格式是您想要的(您的本地系统)。我的帖子中有一个简单的例子。
回答by Krutarth Vora
Jquery version
jQuery 版本
var currentdate = new Date();
$('#DatePickerInputID').val($.datepicker.formatDate('dd-M-y', currentdate));