Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
63
rated 0 times [  67] [ 4]  / answers: 1 / hits: 23715  / 10 Years ago, tue, june 10, 2014, 12:00:00

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'.



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.



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.



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))

More From » jquery

 Answers
10

You are getting the toString value of the new Date. Try this



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



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
}
});

[#70643] Saturday, June 7, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
earllutherf

Total Points: 412
Total Questions: 108
Total Answers: 102

Location: Argentina
Member since Thu, Mar 18, 2021
3 Years ago
;