Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a day with selected date using Jquery datepicker

I have been trying to add a day for another date field with date selected of current field

     ,
 onSelect: function(date) {
     var date2 = $('.currDate').datepicker('getDate');
       date2.setDate(date2.getDate()+1); 
       $('.nextDt').datepicker('setDate', date2);
    }

However I am getting at date2.setDate(date2.getDate()+1);

Message: Object doesn't support this property or method

How can I solve this issue?

like image 545
Jacob Avatar asked Apr 20 '13 07:04

Jacob


People also ask

What is RTL Datepicker?

Right-to-left (RTL) support reflects the ability of a widget to render its content in a right-to-left direction for right-to-left languages, such as Arabic, Hebrew, Chinese, or Japanese. For more information, refer to: RTL support by the DatePicker (demo)

How do I change Datepicker format from DD MM to YYYY?

The jQuery DatePicker plugin supports multiple Date formats and in order to set the dd/MM/yyyy Date format, the dateFormat property needs to be set. The following HTML Markup consists of a TextBox which has been made Read Only.

What is date range picker?

A date range picker helps users select a range between two dates.


2 Answers

It is because, currDate might be empty.

If currDate is emtpy $('.currDate').datepicker('getDate') will return null in which case date2.setDate(date2.getDate()+1); could throw the error

Update:

$(function() {
    $('#nxtDate').datepicker({
        dateFormat: "dd-M-yy", 
    });
    $("#currDate").datepicker({
        dateFormat: "dd-M-yy", 
        minDate:  0,
        onSelect: function(date){
            var date2 = $('#currDate').datepicker('getDate');
            date2.setDate(date2.getDate()+1);
            $('#nxtDate').datepicker('setDate', date2);
        }
    });
})
like image 105
Arun P Johny Avatar answered Oct 12 '22 01:10

Arun P Johny


setDate and getDate are the functions supported by Date() of js while you getDate from datepicker it returns as string so you need to convert it or try this code:

onSelect: function(date) {  
     if(date!=undefined){
         var dateObject=new Date(date);
         dateObject.setDate(dateObject.getDate()+1);                                 
         $('.nextDt').datepicker('setDate', dateObject);
      }
    }

Here is Demo Alerting Current and Next Date

like image 28
Zaheer Ahmed Avatar answered Oct 12 '22 02:10

Zaheer Ahmed