Search code examples
javascriptjquerydatapicker

Add get current date and add 5 days on the next value


I'm trying to create a booking button where's it should get the current date put it on "from" and would add 5 days to put it in "To" day.

so it looks something like this.

$(function() {
    $('#booknow').click(function() {
        fromDate = getCurrentDate(); //DD/MM/YYYY
        toDate = fromDate + 5;
        url = 'http:domain.com/' + fromDate + ToDate + 'moreParameters=1;';

        window.location = url;
        });     
    }
});

BOOK NOW

I'm using jquery.datepick.min.js

How am I able to achieve this?

Edit:

hmm.. Any ideas what's wrong here: http://jsbin.com/ociweb


Solution

  • You can create a Date instance five days after another one like this:

     var fiveDaysLater = new Date( existingDate.getTime() );
     fiveDaysLater.setDate(fiveDaysLater.getDate() + 5);
    

    The "setter" functions on the JavaScript Date prototype know how to interpret such things such that setting the day-of-month to something like 35 correctly leaves you with a date in the following month.

    edit — if all you want is a URL as in your question, formed with the present date and the date five days from today, then:

    $(function() {
        $('#booknow').click(function() {
            var fromDate = new Date(), toDate = new Date();
    
            toDate.setDate(toDate.getDate() + 5);
    
            alert('http:domain.com/' + fromDate + toDate + '?moreParameters=1;');
            // window.location.href = 'http:domain.com/' + fromDate + toDate + '?moreParameters=1;';
    
       });     
    });