Incpetor
Incpetor

Reputation: 1303

Set jQuery Datepicker value restricted to last month only

I have this script for selecting date.

  <script type='text/javascript'>
    $(function () {
        $("#datepicker").datepicker({
            minDate: "-5M",
            maxDate: "-1M",   
            changeMonth: true,
            changeYear: true
        });
    });
    function Image13_onclick() {
    }
</script>

this allows me to select any date from past five months. I want just one month for the user to select and that should be previous month. For example if current month is March then datepicker should only have Feb.

Upvotes: 1

Views: 2976

Answers (1)

Alex Ou
Alex Ou

Reputation: 106

Relative dates must contain value and period pairs; valid periods are "y" for years, "m" for months, "w" for weeks, and "d" for days. For example, "+1m +7d" represents one month and seven days from today. So you only need to calculate the right min date and max date. JS Fiddle

$(function () {
        var date = new Date();
        var maxDate = "-" + date.getDate() + "D";
        var minDate = "-1M " + "-" + (date.getDate() - 1) + "D";;
        $("#datepicker").datepicker({
            minDate: minDate,
            maxDate: maxDate,   
            changeMonth: true,
            changeYear: true
        });
    });

Upvotes: 3

Related Questions