-1

an error with the date selection. When I select a day in the calendar, the month is recorded and I cannot select more than 12. For example, I select January 12, but December 1 comes on the date. Help me please. I think the whole problem is that I create a date and I need to make a date instance

function nextmonths() {
  var start_multi = new Date(document.getElementById("date__start").value);
  var next_date_unformatted = start_multi;
  var next_date_formatted;

  var next_date;
  next_date = new Date(next_date_unformatted);
  console.log(next_date)
  next_date_formatted = next_date.toLocaleDateString()
  document.getElementById("date__end").value = next_date_formatted;
  dateSelected.innerHTML = 'С' + '  ' + start_multi + ' ' + 'по' + ' ' + next_date_formatted;
}
<link href="https://github.com/t1m0n/air-datepicker/blob/v3/dist/air-datepicker.css" rel="stylesheet" />
<script src="https://github.com/t1m0n/air-datepicker/blob/v3/dist/air-datepicker.js"></script>

<div class=" d-flex flex-column ">
  <label for="" class=" insurance__program-label mb-lg-1 mb-1">Дата начала страховки:</label>
  <input type="text" id="date__start" class="dates__form insurance__program-aria" placeholder="06.01.2023">
</div>
<div class="d-flex flex-column mt-lg-2 ">
  <label for="" class="mt-lg-2 mt-3 insurance__program-label mb-lg-1 mb-1">Дата окончания страховки:</label>
  <input type="text" id="date__end" class="dates__form insurance__program-aria" placeholder="12.06.2023">
</div>
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
Адиль
  • 15
  • 5

1 Answers1

-1
function nextmonths() {
  var start_multi = new Date(document.getElementById("date__start").value);
  var next_date_unformatted = new Date(start_multi);
  next_date_unformatted.setMonth(next_date_unformatted.getMonth() + 1);
  var next_date_formatted = next_date_unformatted.toLocaleDateString();
  document.getElementById("date__end").value = next_date_formatted;
  dateSelected.innerHTML = 'С' + '  ' + start_multi.toLocaleDateString() + ' ' + 'по' + ' ' + next_date_formatted;
}
4efirrr
  • 231
  • 1
  • 5
  • Code–only answers aren't that helpful. A good answer should explain why the OP has their issue and how your code fixes it. You should also provide some example input and output, and even better post as a runnable snippet to show the code working. – RobG Mar 15 '23 at 08:14