0

For a few days, I have had a problem with the date processed by JS, which would try to make the year jumps forward. A bit of sample code below.

Inicjal date in HTML 'Jan 19 2022'

  var dateYear = $(this).attr("dateYear");
  console.log('dateYear ' + dateYear); // dateYear 2022

  var dateMonth = $(this).attr("dateMonth");
  console.log('dateMonth ' + dateMonth); // dateMonth 12
  var dateDay = $(this).attr("dateDay");
  console.log('dateDay ' + dateDay); // dateDay 19
    
  var myDate = new Date(dateYear, dateMonth, dateDay);
  console.log('myDate ' + myDate); // myDate Thu Jan 19 2023 00:00:00 GMT+0000 (Greenwich Mean Time)

Even if it subtracts one index from a month, but the year jump to 2023, if the index of the month remains unchanged teddy data will be 'Feb 19 2022';

What could it be for two years there was no problem with the date.

UbuVolt
  • 19
  • 3

1 Answers1

2

This is JavaScript.

January is 0.

December is 11.

See Date.prototype.getMonth().

tao
  • 82,996
  • 16
  • 114
  • 150