简体   繁体   中英

JQuery ui - date picker, Date Formating

I want to impliment the same functionality as in this qeustion JQuery ui - date picker, disabling specific dates , the script working fine(see the code below), but the problem is I am getting Unavailable dates from database, so the dates has a leading zero, ie var unavailableDates = ["90-3-2012", "14-03-2012", "15-03-2012"]; If I test with same dates without leading zero it works, but I want to with leading zero.

How can we format the dates, I am using the exact code from an answer for the above said quesiton , here is the code

<script type="text/javascript">
    var unavailableDates = ["9-3-2012", "14-3-2012", "15-3-2012"];

    function unavailable(date) {
        dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
        if ($.inArray(dmy, unavailableDates) == -1) {
            return [true, ""];
        } else {
            return [false, "", "Unavailable"];
        }
    }

    $(function() {
        $("#iDate").datepicker({
            dateFormat: 'dd MM yy',
            beforeShowDay: unavailable
        });

    });
</script>

You can use datepicker to format date like

 var unavailableDates = ["09-03-2012", "14-03-2012", "15-03-2012", "15-07-2015"]; function unavailable(date) { var dmy = $.datepicker.formatDate('dd-mm-yy', date); console.log(dmy) if ($.inArray(dmy, unavailableDates) == -1) { return [true, ""]; } else { return [false, "", "Unavailable"]; } } $(function() { $("#iDate").datepicker({ dateFormat: 'dd MM yy', beforeShowDay: unavailable }); }); 
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script> <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/redmond/jquery-ui.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script> <input id="iDate" /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM