So I just started programming (I don't know if this is a correct word for it). I started making a website and wanted to add a date and clock. I followed a YouTube tutorial and it works but the month number is one month behind. Is there anyway to fix this problem? Also I wanted to make month number double if its number is less than 10. I wrote the JavaScript code and it messed with the month name (In this case Gegužė).
Asked
Active
Viewed 258 times
-1
-
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth - getMonth returns a 0-based value for the month. So January is 0, December is 11 (who knows why, but that's what it does). So yeah the fix is...add 1 to the month value the Date object outputs, before you use it. – ADyson May 19 '20 at 16:02
-
1This is a duplicate of [getMonth in javascript gives last month](https://stackoverflow.com/questions/18624326/getmonth-in-javascript-gives-last-month) (or multiple others) – ADyson May 19 '20 at 16:03
-
4Please, [don't post image of code](https://meta.stackoverflow.com/a/285557/479251). – Pac0 May 19 '20 at 16:03
-
FYI: _(I don't know if this is a correct word for it)_ "programming " sounds fine, however the more your practice it and build pieces of software, the more you call this "development" (jobs are advertised as "software/web/mobile/etc... **developer**"". – Pac0 May 19 '20 at 16:05
1 Answers
0
The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).
const moonLanding = new Date('July 20, 69 00:20:18');
console.log(moonLanding.getMonth()); // (January gives 0)
// expected output: 6

Rush W.
- 1,321
- 2
- 11
- 19