JavaScript的日期格式为dd / MM / YYYY

时间:2023-02-07 15:48:03

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 .

我想将日期格式更改为“dd / MM / YYYY”,当我更改它时给我另一个错误的日期。 adddate()函数默认设置在加载时的到达日期和出发日期.changedDate()在我更改到达日期时更改出发日期。

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">

2 个解决方案

#1


0  

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">

#2


0  

There is a free library available called moment.js in which you can play with date formatting very easily.

有一个名为moment.js的免费图书馆,您可以在其中轻松播放日期格式。

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

#1


0  

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">

#2


0  

There is a free library available called moment.js in which you can play with date formatting very easily.

有一个名为moment.js的免费图书馆,您可以在其中轻松播放日期格式。

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