3

I am using jquery UI. I want to set last sunday select as default value.

<script>
    $(function() {
        $( "#datepicker" ).datepicker({
            dateFormat: 'dd-mm-yy',
            currentText: "???"
        });
    });

</script>

what to write in place of ??? to achieve this?

1 Answer 1

2

Is this what you're after?

<script>
    function getLastSunday() {
        var sun = new Date();
        sun.setDate(sun.getDate() - sun.getDay());
        return sun;
    }

    $(function() { $( "#datepicker" ).datepicker({ dateFormat: 'dd-mm-yy', defaultDate: getLastSunday() }); });
</script>

The currentText option that you mentioned only specifies the text shown on a button that links to the current day that is only shown if the showButtonPanel option is set to true.

defaultDate, as shown in my code, sets the date that is selected by default when the datepicker is opened.

Alternatively, if you want to actually set the value of the input box, include the getLastSunday() function then use

$(function() {
        $( "#datepicker" ).datepicker({
            dateFormat: 'dd-mm-yy',
        });
        $("#datepicker").datepicker('setDate', getLastSunday());
    });

I've made a jsfiddle here

Basic algorithm taken from: http://forum.jquery.com/topic/datepicker-default-date-set-to-last-week

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.