Question
How can I get the current date in JavaScript?
I want to understand the correct way to retrieve the current date using JavaScript, and how the built-in Date object works for this purpose.
Short Answer
By the end of this page, you will understand how to get the current date in JavaScript using the Date object, how to read parts of a date such as year, month, and day, and how to avoid common mistakes when formatting or interpreting date values.
Concept
In JavaScript, the current date and time are provided by the built-in Date object. To create a date representing right now, you use:
const now = new Date();
This creates a Date instance containing the current date and current time according to the user's system clock.
A common point of confusion is that JavaScript does not have a separate "current date only" type. new Date() always includes both:
- date
- time
- timezone context
If you only want the date portion for display, you usually format the Date object after creating it.
This matters in real programming because dates are used everywhere:
- showing today's date in a UI
- recording timestamps in logs
- validating deadlines
- scheduling tasks
- filtering records by day
Understanding Date is important because many bugs come from assuming dates are stored or displayed the same way everywhere. JavaScript dates can look different depending on locale, timezone, and formatting method.
Mental Model
Think of new Date() as taking a snapshot of the clock on the wall right now.
Dateis the clock/calendar objectnew Date()captures the current moment- methods like
getFullYear()orgetDate()let you inspect parts of that snapshot
So instead of asking JavaScript for "the date" directly, you ask it to create a full timestamp, then you extract the pieces you need.
Syntax and Examples
The basic syntax is:
const now = new Date();
Example: Get the current date object
const now = new Date();
console.log(now);
This prints a Date object representing the current date and time.
Example: Get individual parts of the current date
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();
console.log(year); // e.g. 2026
console.log(month); // 1 to 12
console.log(day); // 1 to 31
Explanation:
Step by Step Execution
Consider this example:
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();
console.log(`${year}-${month}-${day}`);
Step by step:
new Date()creates a date object for the current moment.now.getFullYear()extracts the year, such as2026.now.getMonth()returns a zero-based month value.- If the current month is June, it returns
5.
- If the current month is June, it returns
- Adding
1converts the month into human-readable form.5 + 1becomes6.
now.getDate()gets the day of the month, such as .
Real World Use Cases
Getting the current date is useful in many common programming tasks:
- Displaying today's date on a dashboard or homepage
- Creating timestamps for logs, messages, or audit records
- Checking deadlines such as whether a subscription has expired
- Filtering data to show records created today
- Naming files with the current date, such as reports or exports
- Sending dates to APIs in a standard format
Example: Show today's date in a webpage
const today = new Date().toDateString();
document.body.textContent = `Today is ${today}`;
Example: Create a simple timestamped log
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] Server started`);
Real Codebase Usage
In real projects, developers usually do more than just call new Date(). They combine it with formatting, validation, and consistent timezone handling.
Common patterns
- Store machine-friendly values using ISO strings:
const createdAt = new Date().toISOString();
- Display user-friendly values using locale formatting:
const today = new Date().toLocaleDateString();
- Use one
Dateobject once per operation instead of callingnew Date()many times unnecessarily:
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();
- when comparing dates from APIs or databases.
Common Mistakes
1. Forgetting new
Broken code:
const now = Date();
console.log(now);
This returns a string, not a Date object.
Correct:
const now = new Date();
console.log(now);
2. Forgetting that months are zero-based
Broken code:
const now = new Date();
console.log(now.getMonth());
If it is June, this prints 5, not 6.
Correct:
const month = new Date().getMonth() + ;
Comparisons
| Task | Method | Notes |
|---|---|---|
| Get current date and time | new Date() | Returns a Date object for now |
| Get readable date text | new Date().toDateString() | Local, human-readable |
| Get local formatted date | new Date().toLocaleDateString() | Depends on locale/settings |
| Get API-friendly timestamp | new Date().toISOString() | UTC format |
| Get year only | new Date().getFullYear() | 4-digit year |
| Get month | new Date().getMonth() + 1 |
Cheat Sheet
// Current date and time
const now = new Date();
// Year
now.getFullYear();
// Month (0-11, so add 1 for human-readable month)
now.getMonth() + 1;
// Day of month (1-31)
now.getDate();
// Day of week (0-6)
now.getDay();
// Readable local-style date
now.toDateString();
// Locale-aware date string
now.toLocaleDateString();
// ISO string in UTC
now.toISOString();
Key rules:
- Use
new Date()to get the current moment getMonth()is zero-basedgetDate()is not the same asgetDay()toISOString()uses UTC- Use
padStart(2, '0')when formatting month/day manually
Quick manual format:
const now = new ();
formatted = ;
FAQ
How do I get today's date in JavaScript?
Use new Date() to get the current date and time:
const today = new Date();
How do I get only the current date without the time?
JavaScript Date objects always contain time too, but you can format the output to show only the date:
const today = new Date().toDateString();
Why does getMonth() return the wrong month?
Because getMonth() is zero-based. January is 0, February is 1, and so on. Add 1 for human-readable output.
What is the difference between Date() and new Date()?
Date() returns a string. new Date() returns a Date object with methods like .
Mini Project
Description
Build a small JavaScript utility that prints today's date in multiple useful formats. This demonstrates how to create a current Date object, extract parts of it, and format it for both humans and machines.
Goal
Create a script that shows the current date as a readable string, as YYYY-MM-DD, and as an ISO timestamp.
Requirements
- Create a
Dateobject representing the current moment. - Print a human-readable date.
- Print the date in
YYYY-MM-DDformat. - Print the full ISO timestamp.
- Use zero padding for month and day in the manual format.
Keep learning
Related questions
Adding Table Rows with jQuery: append(), Limits, and Best Practices
Learn how to add table rows in jQuery using append(), what elements are allowed in tables, and safer ways to build rows dynamically.
Bower vs npm: What’s the Difference in JavaScript Package Management?
Learn the plain difference between Bower and npm, how each manages packages, and why npm replaced Bower in most JavaScript projects.
Can `(a == 1 && a == 2 && a == 3)` Ever Be True in JavaScript?
Learn how JavaScript type coercion, loose equality, and custom object conversion can make `a == 1 && a == 2 && a == 3` true.