简体   繁体   中英

How to change the format of JQuery Date Picker to MM/DD/YYYY

Based on this link

This where I will go if i wanna change my date format

defaults: {
    formatDate: function(date) {
        var formatted = $.datePicker.utils.pad(date.getDate(), 2) + '/' + $.datePicker.utils.pad(date.getMonth() + 1, 2) + '/' + date.getFullYear();
        return formatted;
    },
    parseDate: function(string) {
        var date = new Date();
        var parts = string.match(/(\d{1,2})\/(\d{1,2})\/(\d{4})/);
        if ( parts && parts.length == 4 ) {
            date = new Date( parts[3], parts[2] - 1, parts[1] );
        }
        return date;
    },
    selectDate: function(date) {
        return true;
    },
    limitCenturies: true,
    closeOnPick: true,
    appendTo: null
},

How can I make this to MM/DD/YYYY? I tried everything I change it but when Im selecting dates the value goes on different dates

Here is the error

在此处输入图片说明

According to the documentation , this should work:

    defaults: {
        formatDate: function(date) {
    //var formatted = $.datePicker.utils.pad(date.getDate(), 2) + '/' + $.datePicker.utils.pad(date.getMonth() + 1, 2) + '/' +  date.getFullYear();
            var formatted = $.datePicker.utils.pad(date.getMonth() + 1, 2) + '/' + $.datePicker.utils.pad(date.getDate(), 2) + '/' + date.getFullYear();

            return formatted;
        },
        parseDate: function(string) {
            var date = new Date();

            if ( string ) {
                formatted_date = string.split('/');
                date = new Date( formatted_date[2], formatted_date[0] - 1, formatted_date[1] );
            }

            return date;
        },
        selectDate: function(date) {
            return true;
        },
        limitCenturies: true,
        closeOnPick: true,
        appendTo: null
    }

See it in action: Demo .

Try this out

var date = new Date('2014-01-06');
var newDate = date.toString('dd-MM-yy');

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