简体   繁体   中英

Date format by JavaScript to dd/MM/YYYY

I want to change date format to "dd/MM/YYYY" when i change it gives me another wrong date. adddate() function set by default arrival date and departure date on load .changedDate() change departure date when i change arrival date .

 addDate(); function addDate() { date = new Date(); var month = date.getMonth() + 1; var day = date.getDate(); var year = date.getFullYear(); if (document.getElementById('startDate').value == '') { document.getElementById('startDate').value = month + '/' + day + '/' + year; } if (document.getElementById('endDate').value == '') { document.getElementById('endDate').value = month + '/' + (day + 1) + '/' + year; } } function changedDate(){ var arrivalDate = new Date(document.getElementById('startDate').value) ; var departureDate = new Date(document.getElementById('endDate').value) ; if(arrivalDate>=departureDate){ var arrDate = new Date(); arrDate.setDate(arrivalDate.getDate()+1); arrDate.setMonth(arrivalDate.getMonth()+1); arrDate.setFullYear(arrivalDate.getFullYear()); document.getElementById('endDate').value = arrDate.getMonth() + '/' + arrDate.getDate() + '/' + arrDate.getFullYear(); } } 
 <input type="text" id="startDate" style="background-color:#5c677b;height:25px;" name="checkin" placeholder="checkin" onchange="changedDate()"> <input type="text" id="endDate" style="background-color:#5c677b;height:25px;" name="checkout" placeholder="checkout"> 

You have assigned month + date + year. Change it like below.

 addDate(); function addDate() { date = new Date(); var month = date.getMonth() + 1; if (month < 10) month = '0' + month; var day = date.getDate(); if (day < 10) day = '0' + day; var year = date.getFullYear(); if (document.getElementById('startDate').value == '') { document.getElementById('startDate').value = day + '/' + month + '/' + year; } if (document.getElementById('endDate').value == '') { document.getElementById('endDate').value = (day + 1) + '/' + month + '/' + year; } } function changedDate() { var startDate = document.getElementById('startDate').value.split("/"); var endDate = document.getElementById('endDate').value.split("/"); var arrivalDate = new Date(startDate[2], startDate[1] - 1, startDate[0]); var departureDate = new Date(endDate[2], endDate[1] - 1, endDate[0]); if (arrivalDate >= departureDate) { var arrDate = arrivalDate; arrDate.setDate(arrDate.getDate() + 1); var month = arrDate.getMonth() + 1; if (month < 10) month = '0' + month; var day = arrDate.getDate(); if (day < 10) day = '0' + day; var year = arrDate.getFullYear(); document.getElementById('endDate').value = day + '/' + month + '/' + year; } } 
 <input type="text" id="startDate" style="background-color:#5c677b;height:25px;" name="checkin" placeholder="checkin" onchange="changedDate()"> <input type="text" id="endDate" style="background-color:#5c677b;height:25px;" name="checkout" placeholder="checkout"> 

有一个免费的库,称为moment.js ,您可以在其中轻松地使用日期格式。

moment().format('dd/MM/YYYY');

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