简体   繁体   中英

JS: Change Date format returned from DatePicker

I'm not that good at JS, so I have a problem with dates.

I want to set range for my DatePickers - that's why when the date is changed, I get it in the following format:

Wed Jul 08 2015 00:00:00 GMT+0200 (CEST)

I need to change it to the date in format dd/mm/yyyy to use the value later. Till now it looks like following:

var nowTemp = new Date();
$("#datepickerStart").on("changeDate", function (e) {
    minDate = e.date;
    console.log(minDate);  // Wed Jul 08 2015 00:00:00 GMT+0200 (CEST)
//        day = minDate.getDate(); <- doesn't work
//        monthIndex = minDate.getMonth();
//        year = minDate.getFullYear();
});


 $('#datepickerStart').datepicker({
    format: "dd/mm/yyyy",
    startDate : new Date('01/01/2009'),
    endDate : new Date()
 });

 $('#datepickerEnd').datepicker({
    format: "dd/mm/yyyy",
    startDate : minDate,//""+day+"/"+monthIndex+"/"+year,<- tried different approaches
    endDate : new Date()
 });

If I use minDate.format("dd/mm/yyyy"), it returns error "Uncaught TypeError: minDate.format is not a function".

Thanks in advance for help.

A good alternative for doing this is using momenjs library.

Other alternative is that you create a function which formats your date as follow:

 var formatDate = function(d) { var date = d.getDate(); var month = d.getMonth(); var year = d.getFullYear(); var newDate = (date < 10 ? ('0' + date) : date) + '/' + (month < 10 ? ('0' + month) : month) + '/' + year; alert(newDate); } // Usage example var now = new Date(); formatDate(now) 

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