We help IT Professionals succeed at work.

Dynamic date period

darren-w-
darren-w- asked
on
Hi,

Have a form that has a date period defined in a select drop down, at present its Fortnight, Month and 6 Month.

I have this code:

jQuery(function(){
   var dfdate=function(){return calculateDate()}();
    jQuery("#Enter_date").datepicker({
        defaultDate: dfdate,
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        beforeShowDay: jQuery.datepicker.noWeekends,
        minDate: 0
    });
})

function calculateDate(){
    var freq = jQuery('#frequency option:selected').attr('label');
    var twovalue;
    var retString;
    if(freq.indexOf(' ') >= 0){
        twovalue=  freq.split(' ');
    }
    if (freq.indexOf(' ') >= 0){
        twovalue=  freq.split(' ');
        var freqNo = twovalue[0];
        var freqPeriod = twovalue[1].charAt(0).toLowerCase();
        retString = "+"+freqNo+freqPeriod
    }else if(freq.toLowerCase() == "fortnight"){
        retString = "+2w";
    }else{
        retString = "+1"+ freq.charAt(0).toLowerCase();
    }
    return retString;
}

Open in new window


What I would like to happen is that when the selected option changes that the period defaultDate be  defined in as can be returned by the function.

How can this be done?
Comment
Watch Question

Information Systems Manager
CERTIFIED EXPERT
Top Expert 2012
Commented:
Try:
jQuery(function(){
    jQuery("#Enter_date").datepicker({
        defaultDate: dfdate,
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        beforeShowDay: jQuery.datepicker.noWeekends,
        minDate: 0
    });
    jQuery("#frequency").change(function(){
        var freq = jQuery(this).find("option:selected").attr("label");
        var twovalue;
        var retString;
        if(freq.indexOf(' ') >= 0){
            twovalue=  freq.split(' ');
        }
        if (freq.indexOf(' ') >= 0){
            twovalue=  freq.split(' ');
            var freqNo = twovalue[0];
            var freqPeriod = twovalue[1].charAt(0).toLowerCase();
            retString = "+"+freqNo+freqPeriod
        }else if(freq.toLowerCase() == "fortnight"){
            retString = "+2w";
        }else{
            retString = "+1"+ freq.charAt(0).toLowerCase();
        }
        jQuery("#Enter_date").datepicker("option","defaultDate",retString);
    }).change();
})

Open in new window

This assumes that everything in your function worked and this is not tested. If you have problems with this then please could you post the html as well.

Author

Commented:
Thats great, this was what I did.:
jQuery(function(){
    jQuery("#Enter_date").datepicker({
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        beforeShowDay: jQuery.datepicker.noWeekends,
        minDate: 0
    });
    setDate();

    jQuery("#frequency").change(function(){
        setDate();
    })
})

function setDate(){
    var freq = jQuery('#frequency option:selected').attr('label');
    var twovalue;
    var retString;
    if(freq.indexOf(' ') >= 0){
        twovalue=  freq.split(' ');
    }
    if (freq.indexOf(' ') >= 0){
        twovalue=  freq.split(' ');
        var freqNo = twovalue[0];
        var freqPeriod = twovalue[1].charAt(0).toLowerCase();
        retString = "+"+freqNo+freqPeriod
    }else if(freq.toLowerCase() == "fortnight"){
        retString = "+2w";
    }else{
        retString = "+1"+ freq.charAt(0).toLowerCase();
    }
    jQuery("#Enter_date").datepicker("option","minDate",retString);
}

Open in new window


I was after minDate actually
Jon NormanInformation Systems Manager
CERTIFIED EXPERT
Top Expert 2012

Commented:
Ahh minDate makes more sense. Have you thought about what happens if they have something selected that lets them pick an early date which they then change the #frequency which means that date is invalid?

Author

Commented:
Humm, added another question, need to do more donkey work on the date http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_27828186.html

Explore More ContentExplore courses, solutions, and other research materials related to this topic.