Tuesday, 21 November 2023

JS getMonth() need to +1

    var today = new Date();

        var tomorrow = new Date(today);

        tomorrow.setDate(today.getDate() + 1);

1700698715993

tomorrow.getMonth()

10

tomorrow

Wed Nov 22 2023 16:18:35 GMT-0800 (Pacific Standard Time)


https://stackoverflow.com/questions/15799514/why-does-javascript-getmonth-count-from-0-and-getdate-count-from-1

This behavior is because

date is stored in array

I assume it's because it would be easier to reference in an array of names, i.e.

var months = ["January", "February", "March", "April", "May", "June", "July",
         "August", "September", "October", "November", "December"];

var d = new Date();

var namedMonth = months[d.getMonth()];

No comments:

Post a Comment