JavaScript

Date & Time

Master JavaScript dates! Learn to create, format, and manipulate dates and times.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Master JavaScript dates! Learn to create, format, and manipulate dates and times. This hands-on tutorial focuses on practical implementation of date & time concepts.

Date & Time

Handling dates and times is a common task in programming. JavaScript provides the built-in Date object to help us work with time.

1. Creating Dates πŸ“…

What is it? A Date object represents a single moment in time.

How it works: You can create a date in 4 ways:

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

2. Getting Date Info πŸ”

Once you have a Date object, you can extract specific parts (year, month, day, etc.).

Crucial:

  • getMonth() returns 0-11 (Jan is 0).
  • getDay() returns 0-6 (Sunday is 0).
  • getDate() returns 1-31 (Day of the month).
JAVASCRIPT PLAYGROUND
⏳ Loading editor…

3. Formatting Dates πŸ“

Displaying dates to users requires formatting.

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

4. Date Arithmetic βž•βž–

You can modify dates or calculate the difference between them.

Tip: When you subtract two dates, you get the difference in milliseconds.

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

5. Timestamps ⏱️

A timestamp is a single number representing milliseconds since January 1, 1970 (Unix Epoch). It's great for storing dates in databases.

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

Practical Example: "Time Ago" Function ⏳

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

AI Mentor

Confused about "JavaScript Date object, timestamps, date formatting, and date arithmetic"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 4

What does new Date(2024, 0, 1) represent?

Jan 1, 2024
Feb 1, 2024
Dec 1, 2024
Invalid Date

Key Takeaways

βœ… Months are 0-indexed (Jan = 0).
βœ… Date.now() gives current timestamp.
βœ… getTime() converts Date to timestamp.
βœ… toISOString() is standard for APIs.
βœ… Subtract dates to get time difference.

What's Next?

Now let's learn how to handle errors gracefully with Error Handling!

Keep coding! πŸš€