Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Dynamic date period

Posted on 2012-08-14
4
Medium Priority
?
766 Views
Last Modified: 2012-08-14
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?
0
Comment
Question by:darren-w-
  • 2
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
JonNorman earned 2000 total points
ID: 38291609
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.
0
 
LVL 13

Author Comment

by:darren-w-
ID: 38291788
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
0
 
LVL 13

Expert Comment

by:JonNorman
ID: 38292015
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?
0
 
LVL 13

Author Comment

by:darren-w-
ID: 38292036
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question