jQuery Moment.js“无效日期”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21541113/
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
Moment.js "Invalid date"
提问by Sameera Silva
I just tried to make a text input with formatted date. It is working fine on Google Chrome, but when I run it on Mozilla Firefox (26.0) it says: Invalid date
.
我只是尝试使用格式化日期进行文本输入。它在 Google Chrome 上运行良好,但是当我在 Mozilla Firefox (26.0) 上运行它时,它说:Invalid date
.
You can see and run my examplein both browsers and hopefully understand the problem.
您可以在两个浏览器中查看并运行我的示例,希望能理解问题所在。
JS Code:
JS代码:
$( "#h_reg_date" ).datepicker();
$("#h_reg_date").datepicker("option", "dateFormat", "yy-M-dd");
$("#save_data").on("click", function(){
var reg_date = $("#h_reg_date").val();
console.log(reg_date);
reg_date = moment(reg_date).format("YYYY/MM/DD");
console.log(reg_date);
return;
});
HTML:
HTML:
<input type="text" id="h_reg_date"class="abs" style="top:135px; left:150px;" size="10" readonly >
<input type="button" id="save_data" class="abs" style="top:370px; left:-200px;" value="Add Data">
Is there any solution to this problem?
这个问题有什么解决办法吗?
采纳答案by Kevin Bowersox
Change the format of the datepicker
to:
将格式更改datepicker
为:
$("#h_reg_date").datepicker("option", "dateFormat", "yy/mm/dd");
JS Fiddle:http://jsfiddle.net/MMm86/1/
JS小提琴:http : //jsfiddle.net/MMm86/1/
To put the date in your requested format use:
要将日期放入您要求的格式,请使用:
$( "#h_reg_date" ).datepicker();
$("#h_reg_date").datepicker("option", "dateFormat", "yy/M/dd");
$("#save_data").on("click", function(){
var reg_date = $("#h_reg_date").val();
console.log(reg_date);
reg_date = moment(reg_date).format("YYYY/MMM/DD");
console.log(reg_date);
return;
});
JS Fiddle:http://jsfiddle.net/MMm86/2/
JS小提琴:http : //jsfiddle.net/MMm86/2/
回答by Sachin T Sawant
Suppose your date is in format 'DD-MM-YYYY' & you want to display it in DD-MMM format then you can do this by first telling moment function which format your date currently is & then asking it to convert it to required format. Below is the example:
假设您的日期格式为 'DD-MM-YYYY' 并且您想以 DD-MMM 格式显示它,那么您可以通过首先告诉 moment 函数您的日期当前是哪种格式然后要求它将其转换为所需格式来实现. 下面是示例:
var taskDueDate = moment(dueDate, 'DD-MM-YYYY').format('DD-MMM');
This way you can convert your date in any format.
通过这种方式,您可以将日期转换为任何格式。
回答by thescientist
It seems to be a known issue, per their docs. http://momentjs.com/docs/#/parsing/string/
根据他们的文档,这似乎是一个已知问题。 http://momentjs.com/docs/#/parsing/string/
Looks like they suggest passing in the time string and the format.
看起来他们建议传入时间字符串和格式。