views:

521

answers:

3

I'm using Kevin Luck's jquery date picker plugin and I'm wondering what is the best way to format the dpSetEndDate() value to always be one year in the future? I tried using php:

<?php echo date('Y')+1;?>-<?php echo date('m-d');?>

But that didn't work. By itself, the php returns what I want to put in place of the date, but it doesn't render within the javascript.

$('.date-picker').dpSetEndDate('01/01/2010');

For reference his site is here: http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/index.html

A: 
    var today = (new Date),
        nextYear = today.getFullYear()+1,
        yearAhead = new Date( today.setYear( nextYear ) )

/*
    yearAhead.getDate() // 27
    yearAhead.getMonth()+1 // 7
    yearAhead.getFullYear() // 2010

*/

    dpSetEndDate( yearAhead.getMonth()+1 + '/' + yearAhead.getDate() + '/' + yearAhead.getFullYear() );

Hopefully I did the month part correctly..

meder
...and if you run this on 29th Feb?
Craig McQueen
If you run this on the 29th ( leap year ) it will account the next year in advance so an initial February 29 2010 will result in the yearAhead date object being March 1st.
meder
A: 

Hi, i am using the same plugin and i was trying to achieve the same goal. What i did was to use little bit of php and add

endDate:'<?=date('d/m/Y', strtotime("+2 Years"))?>'

on the script declaration and everything works fine

A: 

You already have an answer and it's an old question but just for reference this is the recommended way:

$('.selector').datePicker(
    {
        endDate: (new Date()).addYears(1).asString()
    }
);

There are a number of useful methods added to the Date object in date.js and you can use these to easily pass values to the datePicker when you initialise it. Date.asString will always format the date in a string in the current Date.format which is the format that the settings object is expecting...

vitch