javascript Jquery datepicker 返回 NAN 作为时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22015259/
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
Jquery datepicker returns NAN as timestamp
提问by sunny_side_of_life
I am using jquery datepickerto select a date and convert to timestamp (epoch time). The script works arbitrarily. It shows timestamp for dates from 1st to 12th of any chosen month but makes time 12:00am of those days (I converted it online). But from 13th to the end of the month it shows NANas timestamp. Strange enough when the date formated to dd/mm/yy all the days shows correctly.
我正在使用jquery datepicker选择一个日期并转换为时间戳(纪元时间)。该脚本可以任意工作。它显示任何选定月份的 1 日至 12 日的时间戳,但将时间设为这些天的凌晨 12:00(我将其在线转换)。但是从 13 号到月底它显示NAN作为时间戳。当日期格式为 dd/mm/yy 所有天都正确显示时,这很奇怪。
$(function() {
$("#datepicker").datepicker({dateFormat: 'dd-mm-yy',
onSelect: function(dateText, inst) {
var dtV = $(this).val();
var d = new Date(dtV);
var s = parseInt((d)/1000);
$("#selectedDate").text("on " + dateText + "");
$(".selectedDate2").text(s.valueOf());
}
});
});
采纳答案by ojovirtual
When you call new Date()
you have to pass a valid date. dd-mm-yy
is not a valid date for Date
and it's chenging your month per day.
当你打电话时,new Date()
你必须通过一个有效的日期。dd-mm-yy
不是一个有效的日期Date
,它每天都在改变你的月份。
If you cannot change your date format, try this:
如果您无法更改日期格式,请尝试以下操作:
onSelect: function(dateText, inst) {
var dtV = $(this).val();
var exploded=dtV.split("-");
var d = new Date(exploded[2],exploded[1],exploded[0]);
EDIT: Better and shorter, use datepicker's getDate
to get a Date
object:
编辑:更好更短,使用日期选择器getDate
来获取Date
对象:
onSelect: function(dateText, inst) {
var d = $(this).datepicker("getDate");
回答by J0ANMM
I had a similar problem: usually the datepicker worked properly, also for dates with the day after the 12th; but from time to time I saw in Google Analytics that a user was getting NaN-NaN-NaN.
我有一个类似的问题:通常日期选择器工作正常,也适用于 12 日之后的日期;但有时我在谷歌分析中看到用户得到NaN-NaN-NaN。
After investigating, I discovered that it could be due to Google Translate.
经过调查,我发现这可能是由于谷歌翻译。
I checked in my analytics the language of the users getting the error. Almost all of them had a language that my website did not support. That was enough to assume that Google Translator was causing the error.
我在我的分析中检查了收到错误的用户的语言。几乎所有人都有我的网站不支持的语言。这足以假设是谷歌翻译导致了错误。
To solve it, as explained here, add the class:
要解决它,如解释here,添加类:
$(function() {
$(".datepicker").datepicker();
$('.ui-datepicker').addClass('');
});
回答by Michael Benjamin
It's interpreting your format as if it's mm-dd-yy
; JavaScript's native Date
class assumes all string input in the format \d\d[-/]\d\d[-/]\d\d
is mm-dd-yy
.
它正在解释您的格式,好像它是mm-dd-yy
; JavaScript 的本机Date
类假定格式\d\d[-/]\d\d[-/]\d\d
为mm-dd-yy
.
The yyyy-mm-dd
format ISO 8601is more common and should be interpreted correctly.
该yyyy-mm-dd
格式ISO 8601是比较常见的,应予以正确解释。
回答by Muhammad Awais
Following works for me. Just added a ui-datepickerclass:
以下对我有用。刚刚添加了一个ui-datepicker类:
$(function () {
$("#txtStartDate").datepicker();
$('.ui-datepicker').addClass('');
});