jQuery 在 input type="date" 中设置格式和值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16770615/
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
Setting format and value in input type="date"
提问by muneebShabbir
Is there any way to set the format of <input type="date" />
? if no then how can i set date in this field using JavaScript in the default format of type="date"
. how can i get what will be the format of this field?
有没有办法设置格式<input type="date" />
?如果没有,那么我如何使用 JavaScript 以默认格式在此字段中设置日期type="date"
。我怎样才能得到这个字段的格式?
EDIT :Actually i want to show native date-picker of mobile device that's why i picked this type of input. if is there any alternate for that field that also will b good to have.
编辑:实际上我想显示移动设备的本机日期选择器,这就是我选择这种类型的输入的原因。如果该领域有任何替代方案,也将是很好的选择。
Sorry if i said anything stupid. Please guide me
对不起,如果我说了什么愚蠢的话。请指导我
回答by wachme
The format is YYYY-MM-DD
. You cannot change it.
格式为YYYY-MM-DD
. 你不能改变它。
$('#myinput').val('2013-12-31');
sets value
$('#myinput').val('2013-12-31');
设定值
回答by Sarkis Arutiunian
new Date().toISOString().split('T')[0];
new Date().toISOString().split('T')[0];
回答by argie cruz
try this :)
尝试这个 :)
function getDefaultDate(){
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) ;
return today;
}
$(document).ready(function(){
$("#dateid").val( getDefaultDate());
});
回答by c0nstruct0r
It's ugly, but it works. :/
这很丑陋,但它有效。:/
var today = new Date().toLocaleString('en-GB').split(' ')[0].split('/').reverse().join('-');
回答by zevero
Easier than the above is
比上面更容易
var today = new Date().toISOString().substring(0,10); # "2013-12-31"
回答by MathJ
Canadian locale happens to follow the same format, too:
加拿大语言环境也恰好遵循相同的格式:
new Date().toLocaleDateString('en-CA')
回答by Ted
I would try jquery and jquery ui to add a nice calendar control that does all the value selecting and formatting for you. simple formatting of an input field isn't enough for demanding users of today's web :)
我会尝试 jquery 和 jquery ui 添加一个漂亮的日历控件,它为您执行所有值选择和格式设置。对于当今网络要求苛刻的用户来说,简单的输入字段格式是不够的:)
Have a look at this link
看看这个链接
回答by Sender
Please check this https://stackoverflow.com/a/9519493/1074944and try this way also $('input[type="date"]').datepicker().prop('type','text');
check the demo
请检查这个https://stackoverflow.com/a/9519493/1074944并尝试这种方式也$('input[type="date"]').datepicker().prop('type','text');
检查演示
回答by i.Nemiro
I think this can help
我认为这可以帮助
function myFormatDateFunction(date, format) {
...
}
jQuery('input[type="date"]')
.each(function(){
Object.defineProperty(this,'value',{
get: function() {
return myFormatDateFunction(this.valueAsDate, 'dd.mm.yyyy');
},
configurable: true,
enumerable : true
});
});
回答by sushant
function getDefaultDate(curDate){
var dt = new Date(curDate);`enter code here`
var date = dt.getDate();
var month = dt.getMonth();
var year = dt.getFullYear();
if (month.toString().length == 1) {
month = "0" + month
}
if (date.toString().length == 1) {
date = "0" + date
}
return year.toString() + "-" + month.toString() + "-" + date.toString();
}
In functionpass your date string.
在函数中传递您的日期字符串。