0

I would like to have a drop down menu for quick picking of a date range. So for example, 'Last week', 'Last month', 'Last three months'. That kind of stuff. On picking one of these it will populate 2 text boxes with the from and to dates from the selection. Have tried some stuff with JUI datepicker but after a Google it seems it is not the way to do it.

Any help would be much appreciated.

Thanks.

1 Answer 1

1

I did something similar, but with radio buttons. Hope it helps:

    function reset_last_checkboxes() {
        $("#export_last_month").attr("checked", false);
        $("#export_3_last_months").attr("checked", false);
        $("#export_last_year").attr("checked", false);
    }
    var date_format = "DD, d MM, yy";
    $("#id_from_date").attr("readonly", "readonly");
    $("#id_till_date").attr("readonly", "readonly");
    $("#id_from_date").datepicker();
    $("#id_till_date").datepicker();
    $( "#id_from_date" ).change(function() {
        $( "#id_from_date" ).datepicker( "option", "dateFormat", date_format );
    });
    $( "#id_till_date" ).change(function() {
        $( "#id_till_date" ).datepicker( "option", "dateFormat", date_format );
    });
    $( "#id_from_date" ).click(function() {
        reset_last_checkboxes();
    });
    $( "#id_till_date" ).click(function() {
        reset_last_checkboxes();
    });
    $("#id_till_date").val(format_date(new Date(now)));
    $("#export_last_month").click(function(){
        var dt = new Date(now);
        $("#id_till_date").val(format_date(dt));
        $("#id_from_date").val(format_date(prevMonth(dt)));
    });
    $("#export_3_last_months").click(function(){
        var dt = new Date(now);
        $("#id_till_date").val(format_date(dt));
        $("#id_from_date").val(format_date(prevMonth(prevMonth(prevMonth(dt)))));
    });
    $("#export_last_year").click(function(){
        var dt = new Date(now);
        $("#id_till_date").val(format_date(dt));
        for (var i=0;i<12;i++) {
            dt = prevMonth(dt);
        }
        $("#id_from_date").val(format_date(dt));
    });

You can always replace my radio buttons with drop down menu and write this code better than me :)

By the way, prevMonth I calculate this way:

function prevMonth(dt){
    var thisMonth = dt.getMonth();
    dt.setMonth(thisMonth-1);
    if(dt.getMonth() != thisMonth-1 && (dt.getMonth() != 11 || (thisMonth == 11 && dt.getDate() == 1)))
        dt.setDate(0);
    return dt;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.