Creating and Reading Dates
Construct Date objects and read components.
Creating and Reading Dates is a free JavaScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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())Frequently asked questions
Is the “Creating and Reading Dates” lesson free?
Yes — the full text of “Creating and Reading Dates” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
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
- Creating and Reading Dates
- Timestamps and Date Math
- Formatting Dates
- Time Zones and UTC