Ad space (banner)
🟨JavaScript Lessons
Lesson 37 / 69

Building and Displaying Dates (Basic Year/Month/Day Operations)

In this lesson you'll learn the basics of date manipulation using JavaScript's Date object, so you can correctly build and display a year, month, day, and day of the week. This is for people searching "JavaScript Date usage" or "JavaScript how to get a date."

The Date object lets you build and work with a specific calendar date. As in new Date(year, month-1, day), remember that months are counted starting from 0 (0 for January). This quirky detail is one of the most common beginner stumbling blocks. You can pull out each piece of information with methods like .getFullYear(), .getMonth(), .getDate(), and .getDay().

The sample code uses new Date(2026, 7, 9) to create a date object representing August 9, 2026, and converts .getMonth() + 1 back into the actual month number. .getDay() returns the day of the week as a number from 0 (Sunday) to 6 (Saturday), so it's combined with an array of weekday names to display "Sun, Mon, Tue, Wed, Thu, Fri, Sat."

A common beginner stumbling block is forgetting that months are counted from 0, and building a date that's a month off from what was intended. A mistake like "wanting to create August but specifying 8 instead of 7, ending up with September" happens very often, so always keep "month minus 1" in mind. Also be careful that how time is handled can vary by time zone.

This is essential basic knowledge for any app dealing with dates, such as a reservation system or an event calendar, and it's also frequently used for calculations involving the day of the week. It's foundational knowledge you can't do without when building your own reservation system or calendar feature.

JavaScript
OUTPUT

💡 Anything passed to console.log() appears in the output below.

Ad space (banner)
Ad space (in-article)