0

I am using Kendodatepicker.below is my code.

<input id="datePicker" />
$(document).ready(function(){
 $("#datePicker").kendoDatePicker({
  value: new Date(),
  min: new Date(1950, 0, 1),
  max: new Date(2049, 11, 31)
 })
});

I want to put the datepicker default selected value as last month start date .please give me an idea. Thanks in advance

sid
  • 99
  • 1
  • 4
  • 14
  • I do recommend take a look into http://stackoverflow.com/a/605121/1802671 as a solution for getting the first day of previous month including months as January and other corner cases. – OnaBai Nov 26 '12 at 22:52

2 Answers2

2

try

$(document).ready(function(){
 var d = new Date().setDate(-1);

 $("#datePicker").kendoDatePicker({
   value: new Date(d),
   min: new Date(1950, 0, 1),
   max: new Date(2049, 11, 31)
 })
});

or

$(document).ready(function(){     
  var d = new Date().setDate(-1);

  $("#datePicker").attr('value',new Date(d));
  $("#datepicker").kendoDatePicker();
});

probably it works in super short way:

$(document).ready(function(){     
   $("#datePicker").kendoDatePicker({
     value: new Date().setDate(-1),
     min: new Date(1950, 0, 1),
     max: new Date(2049, 11, 31)
   })
 });

have fun

theforce
  • 1,505
  • 1
  • 11
  • 13
  • `setDate(-1)` is the last day of previous month, isn't it? I think that Sid was asking by the first and not the last. – OnaBai Nov 26 '12 at 22:47
1

First get current date using Date() and then subtract a month and then set it to the first of the month.

$(document).ready(function() {

var lastMonthStart = new Date();
with(lastMonthStart) {
    setMonth(getMonth()-1);
    setDate(1);
}
$("#datePicker").kendoDatePicker({
    value: lastMonthStart,
    min: new Date(1950, 0, 1),
    max: new Date(2049, 11, 31)
}) });

Should give you the start of last month.

RogueThinking
  • 164
  • 1
  • 5