Javascript/Jquery 的默认日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24133819/
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
Default date format of Javascript/Jquery
提问by Dhwani
I have a kendo date picker which is set to format date as "MM/dd/yyyy". I want to check using jquery/javascript that if kendo date picker date must not be future date and date must be greater than '01/01/1900'.
我有一个剑道日期选择器,它被设置为将日期格式化为“ MM/dd/yyyy”。我想使用 jquery/javascript 检查是否剑道日期选择器日期不能是未来日期并且日期必须大于“01/01/1900”。
The issue I am facing is when I take new date in script, it is like Date {Tue Jun 10 2014 11:17:48 GMT+0530 (India Standard Time)}
. and my kendo date picker has value in 06/02/2012
format. I don't know how to compare it.
我面临的问题是当我在脚本中获取新日期时,就像Date {Tue Jun 10 2014 11:17:48 GMT+0530 (India Standard Time)}
. 我的剑道日期选择器在06/02/2012
格式上有价值。我不知道怎么比较。
I know a method in kendo date picker named: parseFormats in which I have to give parsing format, but I don't know defualt date format of Javascript/Jquery and I don't know how to do it.
我知道剑道日期选择器中的一个方法,名为:parseFormats,我必须在其中提供解析格式,但我不知道 Javascript/Jquery 的默认日期格式,也不知道该怎么做。
Kendo Date Picker
剑道日期选择器
@(Html.Kendo().DatePickerFor(m => m.DateOfBirth).Format("MM/dd/yyyy").HtmlAttributes(new { style = "width:100%", Placeholder = "mm/dd/yyyy" }).Max(DateTime.Now.Date).Min(new DateTime(1900, 1, 2).Date))
采纳答案by mplungjan
You are getting the toString value of the new Date. Try this
您正在获取新日期的 toString 值。试试这个
var d = new Date(datepicker.value()); // looked at the docs, no obvious shortcut
if (d.getFullYear()<1900) alert("nope");
or
或者
var now = new Date(), d = new Date(datepicker.value());
now.setHours(0,0,0,0); // normalise
if (d.getTime() > now.getTime()) alert("Please no future dates");
More information about JS Dates: MDN
有关 JS 日期的更多信息:MDN
You can also make it harder to select the invalid dates
您还可以使选择无效日期变得更加困难
$("#datetimepicker").kendoDateTimePicker({
value:new Date(),
min: new Date(1900,0,1), // JS months are 0 based!
max: new Date()
});
And lastly add the validation
最后添加验证
$("#MyForm").kendoValidator({
rules: {
//implement your custom date validation
dateValidation: function (dateField) {
var currentDate = Date.parse($(dateField).val());
//Check if Date parse is successful
if (!currentDate) {
return false;
}
var now = new Date(), then=new Date(1900,0,1),d = new Date($(dateField).val());
now.setHours(0,0,0,0); // normalise
return d.getTime() >= then.getTime() && d.getTime() < now.getTime()
}
},
messages: {
//Define your custom validation massages
required: "Date is required message",
dateValidation: "Invalid date message"
}
});
回答by Arijit Mukherjee
Default Date Format of Javascript is MM/DD/YYYY
Javascript 的默认日期格式为 MM/DD/YYYY
For Reference Follow Date Format
供参考遵循日期格式
回答by vapcguy
I would pretty much ignore Kendo's methods altogether and use moment.jsin a validation function when you submit. You can format each date, min, max, and candidate, as YYYY-MM-DD, then compare using built-in .isAfter()
and .diff()
queries. Remember that you have to account for if they type something, not just pick it from the calendar, so you have to ensure you have 2-digit days. You also have to account for if someone enters in something outrageous that is higher than the Kendo control can deal with, like 1/1/0001 and 1/1/9000. Code below deals with that. You may also - though I did not include it here in my code, but did in my fiddle - want to account for if the year is only 2-digits, as well:
我几乎会完全忽略 Kendo 的方法,并在您提交时在验证函数中使用moment.js。您可以将每个日期、最小值、最大值和候选格式设置为 YYYY-MM-DD,然后使用内置.isAfter()
和.diff()
查询进行比较。请记住,您必须考虑他们是否输入内容,而不仅仅是从日历中挑选,因此您必须确保您有两位数的天数。您还必须考虑是否有人输入了高于 Kendo 控制可以处理的令人发指的内容,例如 1/1/0001 和 1/1/9000。下面的代码处理这个。您也可能 - 虽然我没有在我的代码中包含它,但在我的小提琴中做了 - 也想考虑年份是否只有两位数:
$('#btnValidate').click(function(){
var minDate = moment('1900-1-1');
var maxDate = moment(Date.parse(new Date()));
//var dateField = $("#datepicker").data("kendoDatePicker").value();
var dateField = $("#datepicker").val();
// Moment requires YYYY-MM-DD
dateField = dateField.replace('/','-').replace('/','-');
var year = dateField.split('-')[2];
var month = dateField.split('-')[0];
var day = dateField.split('-')[1];
if (month < 10 && month.toString().length == 1) { month = "0" + month; }
if (day < 10 && day.toString().length == 1) { day = "0" + day; }
dateField = year + '-' + month + '-' + day;
// Enter into moment and compare
var dateToConsider = moment(dateField);
var lowerLimitBreached = dateToConsider.diff(minDate) < 0;
var upperBoundBreached = dateToConsider.isAfter(maxDate);
alert('min: ' + moment(minDate).format('MM/DD/YYYY'));
alert('max: ' + moment(maxDate).format('MM/DD/YYYY'));
alert('our candidate: ' + moment(dateToConsider).format('MM/DD/YYYY'));
if(lowerLimitBreached || upperBoundBreached)
alert('Invalid date');
else
alert('Valid date');
});
http://jsfiddle.net/navyjax2/k5xx9xpu/
http://jsfiddle.net/navyjax2/k5xx9xpu/
Note that the example doesn't show using times, but you could add that if you got the time from the .data("kendoDatePicker").value
commented-out line. I just would not trust the year since "0001" will translate as "1901". So I would say that appending the time to the dateField
object would be the way to go, and you can hard-code the time on it like moment(year + '-' + month + '-' + day + 'T' + hours + ':' + mins + ':' + secs + 'Z').utc()
and the min like moment('1900-1-1T00:00:00Z')
, though 00:00:00Z
is already implied if you do not set it.
请注意,该示例未显示使用时间,但如果您从.data("kendoDatePicker").value
注释掉的行中获得时间,则可以添加它。我只是不相信年份,因为“0001”将翻译为“1901”。所以我会说将时间附加到dateField
对象将是要走的路,你可以硬编码它的时间 likemoment(year + '-' + month + '-' + day + 'T' + hours + ':' + mins + ':' + secs + 'Z').utc()
和 min like moment('1900-1-1T00:00:00Z')
,但00:00:00Z
如果你不设置它已经暗示了。
回答by Chirag Vidani
You can use KendoUI's datepicker method as shown below:
您可以使用 KendoUI 的 datepicker 方法,如下所示:
var datepicker = $("#datepicker").data("kendoDatePicker");
var value = datepicker.value();
Here value will be holding value like Tue Oct 11 2015 11:17:48 GMT+0530 (India Standard Time)
这里的价值将是持有价值 Tue Oct 11 2015 11:17:48 GMT+0530 (India Standard Time)
Now you can use simple condition to compare values
现在您可以使用简单的条件来比较值
EDIT
编辑
You can refer demo at this fiddle
你可以参考这个小提琴的演示
Once you have javascript date format you can use condition to compare dates, as I have in demo code
一旦你有了 javascript 日期格式,你就可以使用条件来比较日期,就像我在演示代码中所做的那样