Automatic date adjustment in JavaScript

JavaScript has a very interesting and useful feature: if an incorrect point in time was specified when creating the Date object, it will automatically be recalculated to the correct one. Let's look at an example.

As you know, there is no date of January 35th. The maximum possible day in January is 31. It turns out that our date January 35 has 4 extra days. JavaScript in this case will simply add those extra 4 days to the next month:

let date = new Date(2018, 0, 35); console.log(date); // will be February 4th

You can specify not only extra days, but also months. It should be remembered that months start from zero, which means that the last valid month is 11th. If you specify 12th month, you get January of the next year:

let date = new Date(2018, 12, 1); // indicates the 12th month console.log(date); // shows January 1, 2019

The described adjustment also works in a smaller direction. As you know, the smallest day of the month is the day numbered 1. Therefore, if you specify a day with the number 0, then you get the last day of the previous month:

let date = new Date(2018, 1, 0); // sets zero day console.log(date); // will be January 31st

You can also specify negative days and months. It must be remembered that the minimum day has the number 1, and the minimum month has the number 0. This means that minus the first day falls on 2d day since the end of the previous month, and minus the first month just falls on the last month of the previous year:

let date = new Date(2018, 1, -1); // sets -1 day console.log(date); // will be January 30th
let date = new Date(2018, -1, 1); // sets -1 month console.log(date); // shows December 1, 2017

All of the above works similarly with hours, minutes and seconds:

let date = new Date(2018, 0, 1, 24, 0, 0); // indicates the 24th hour console.log(date); // will be January 2nd, 0 hours
let date = new Date(2018, 0, 1, 25, 0, 0); // indicates the 25th hour console.log(date); // will be January 2nd, 1 hour

Determine what date JavaScript will convert the next point in time to:

let date = new Date(2018, 1, 35); console.log(date); // what will be the date?

Determine what date JavaScript will convert the next point in time to:

let date = new Date(2018, 15, 1); console.log(date); // what will be the date?

Determine what date JavaScript will convert the next point in time to:

let date = new Date(2018, 3, 31); console.log(date); // what will be the date?

Determine what date JavaScript will convert the next point in time to:

let date = new Date(2018, 1, 31); console.log(date); // what will be the date?

Determine what date JavaScript will convert the next point in time to:

let date = new Date(2018, 12, 33); console.log(date); // what will be the date?

Determine what date JavaScript will convert the next point in time to:

let date = new Date(2018, 33, 33); console.log(date); // what will be the date?

Determine what date JavaScript will convert the next point in time to:

let date = new Date(2018, 5, 0); console.log(date); // what will be the date?

Determine what date JavaScript will convert the next point in time to:

let date = new Date(2018, 0, 0); console.log(date); // what will be the date?

Determine what date JavaScript will convert the next point in time to:

let date = new Date(2018, -12, -33); console.log(date); // what will be the date?

Determine what date JavaScript will convert the next point in time to:

let date = new Date(2018, 0, 1, 23, 0, 60); console.log(date); // what will be the date?

Determine what date JavaScript will convert the next point in time to:

let date = new Date(2018, 0, 1, 23, 60, 0); console.log(date); // what will be the date?

Determine what date JavaScript will convert the next point in time to:

let date = new Date(2018, 0, 1, 100, 100, 100); console.log(date); // what will be the date?
enru