Search code examples
jquerydatejquery-tools

jquery tools dateinput : addDay(amount)


I am using jquery tools date input. In my application i have a feature that allows the user to check his personal data based on date range.

I have an input text field which loads the date Input functionality. When the user select a date it displays it in : "dddd dd, mmmm yyyy" format in the text field.
What i am trying to do is to add 7 days (ie a week) and display the new date in the upper range container, to explain more this is how it looks:

From: Friday 18, May 2012 To: //here is where i want it to be : Friday 25, May 2012

I am trying to use the built in api function :addDay(amount) but it seems not to work. I have the following code:

var ret_date = $(this).data("dateinput").getValue('dddd dd, mmmm yyyy');
//I tried: $(this).data("dateinput").addDay(7).getValue('dddd dd, mmmm yyyy')
//but it kept the same currently selected date
$("#to_field").html(ret_date);

I searched online and SO but couldn't find a solution. I am not generally a front end developer so my knowledge with javascript/jquery is not advanced.
Any help is much appreciated


Solution

  • By the way, this is what i ended up doing and it worked:

    $("#DateField").change(function(event, date) {
            //I used format: yyyy-mm-dd
            var ret_date= $(this).data("dateinput").getValue('yyyy-mm-dd');   
            //Format the date using javascript
            //--> For that 2 javascript prototypes have been created(check at the end of the answer)          
            var myDate = new Date(ret_date);
            myDate.setDate(myDate.getDate()+7);
            $("#FinalDateField").html(myDate.dateFormat("Y-m-d"));
    });
    

    The prototypes:

    Date.prototype.dateFormat = function(format) {
      var result = "";
      for (var i = 0; i < format.length; ++i) {
        result += this.dateToString(format.charAt(i));
      }
      return result;
    }
    
    Date.prototype.dateToString = function(char) {
      switch (char) {
      case "Y":
        return this.getFullYear();
      case "m":
        return this.getMonth() + 1; //Since months are zero based, add 1
      case "d":
        return this.getDate();
      default:
        return char;
     }
    }