JavaScript Academy · Lesson

Creating and Reading Dates

Construct Date objects and read components.

Lesson 1 of 413 steps

Creating and Reading Dates is a free JavaScript Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.

The Date Object

The Date object represents a single moment in time. You create one with the Date constructor using new.

const now = new Date()
console.log(now instanceof Date)

Current Date

Calling new Date with no arguments captures the current moment according to the system clock.

const now = new Date()
console.log(typeof now.getTime())

From a Date String

You can build a date from an ISO date string. The year, month, and day are parsed from the text.

const d = new Date("2024-03-15")
console.log(d.getFullYear())

From Components

You can pass individual components: year, month, day, and optionally hours and minutes. Remember that the month argument is zero-based.

const d = new Date(2024, 0, 15)
console.log(d.getFullYear())
console.log(d.getDate())

getFullYear

getFullYear returns the four-digit year. Always use it instead of the deprecated getYear.

const d = new Date("2024-07-04")
console.log(d.getFullYear())

getMonth Is Zero-Based

getMonth returns a number from 0 to 11, where 0 is January and 11 is December. This is a common source of off-by-one bugs.

const d = new Date("2024-07-04")
console.log(d.getMonth())

Converting Month to Name

Because months are zero-based, you index into a names array directly with getMonth.

const names = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
const d = new Date(2024, 6, 4)
console.log(names[d.getMonth()])

getDate and getDay

getDate returns the day of the month from 1 to 31, while getDay returns the weekday from 0 for Sunday to 6 for Saturday. Do not confuse the two.

const d = new Date(2024, 0, 1)
console.log(d.getDate())
console.log(d.getDay())

Time Components

Use getHours, getMinutes, and getSeconds to read the time portion of a date.

const d = new Date(2024, 0, 1, 9, 30, 15)
console.log(d.getHours())
console.log(d.getMinutes())
console.log(d.getSeconds())

Building a Readable String

Combine the getter methods to format a date manually, remembering to add one to the month.

const d = new Date(2024, 6, 4)
const text = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate()
console.log(text)

Invalid Dates

An unparseable date string produces an Invalid Date whose time is NaN. Check with Number.isNaN on getTime to detect it.

const bad = new Date("not a date")
console.log(Number.isNaN(bad.getTime()))

Quick Check

Test your date reading knowledge.

Recap

Recap: Date objects hold a moment in time.

  • new Date with no args is now
  • getMonth is zero-based
  • getDate is day of month; getDay is weekday
  • Invalid dates have a NaN time
const d = new Date(2024, 0, 15)
console.log(d.getMonth())
Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “Creating and Reading Dates” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “Creating and Reading Dates”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.

What will I learn in “Creating and Reading Dates”?

Construct Date objects and read components. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Creating and Reading Dates” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating and Reading Dates
  2. Timestamps and Date Math
  3. Formatting Dates
  4. Time Zones and UTC
← Back to JavaScript Academy