javascriptjquerydatekendo-uikendo-datepicker

Default date format of Javascript/Jquery


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

Solution

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