Question

How to Get the Current Date in JavaScript

javascriptdatedate-formatting

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.

  • Date is the clock/calendar object
  • new Date() captures the current moment
  • methods like getFullYear() or getDate() 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.

Take Quiz

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:

  • getFullYear() returns the 4-digit year
  • getMonth() returns 0 for January through 11 for December
  • getDate() returns the day of the month

Example: Get a readable current date string

const today = new Date().toDateString();
console.log(today);

Possible output:

"Wed Jun 10 2026"

Example: Get an ISO-style date

const today = new Date().toISOString();
console.log(today);

Possible output:

"2026-06-10T14:30:00.000Z"

This is useful when sending dates to APIs, but note that toISOString() uses UTC, not local time.

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:

  1. new Date() creates a date object for the current moment.
  2. now.getFullYear() extracts the year, such as 2026.
  3. now.getMonth() returns a zero-based month value.
    • If the current month is June, it returns 5.
  4. Adding 1 converts the month into human-readable form.
    • 5 + 1 becomes 6.
  5. now.getDate() gets the day of the month, such as 10.
  6. The template literal combines the values into a string like:
"2026-6-10"

If you need leading zeros, you can pad the values:

const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');

console.log(`${year}-${month}-${day}`);

This produces:

"2026-06-10"

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`);
Take Quiz

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 Date object once per operation instead of calling new Date() many times unnecessarily:
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();
  • Guard against timezone confusion when comparing dates from APIs or databases.

Example: Validation pattern

function isExpired(expiryDateString) {
  const now = new Date();
  const expiry = new Date(expiryDateString);
  return now > expiry;
}

Example: Configuration and logging

function logMessage(message) {
  const timestamp = new Date().toISOString();
  console.log(`[${timestamp}] ${message}`);
}

In codebases, the key idea is consistency:

  • use local formatting for UI
  • use ISO or UTC for storage and APIs
  • be explicit about what timezone you mean

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() + 1;

3. Confusing getDate() with getDay()

  • getDate() = day of the month
  • getDay() = day of the week (0 to 6)

Broken code:

const now = new Date();
console.log(now.getDay());

This does not give the day of the month.

4. Assuming toISOString() uses local time

const iso = new Date().toISOString();

This gives a UTC timestamp. In some timezones, the date part may appear different from your local date.

5. Building date strings without zero padding

const now = new Date();
const result = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`;

This may produce 2026-6-3 instead of 2026-06-03.

Better:

const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');

const result = `${year}-${month}-${day}`;

Comparisons

TaskMethodNotes
Get current date and timenew Date()Returns a Date object for now
Get readable date textnew Date().toDateString()Local, human-readable
Get local formatted datenew Date().toLocaleDateString()Depends on locale/settings
Get API-friendly timestampnew Date().toISOString()UTC format
Get year onlynew Date().getFullYear()4-digit year
Get monthnew Date().getMonth() + 1Must add 1
Get day of monthnew Date().getDate()1 to 31

Date() vs new Date()

ExpressionResult
Date()A string of the current date/time
new Date()A real Date object

getDate() vs getDay()

MethodMeaning
getDate()Day of the month
getDay()Day of the week

Local time vs UTC

MethodTimezone
new Date() display in many contextsUsually local environment representation
toLocaleDateString()Local timezone
toISOString()UTC
Take Quiz

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-based
  • getDate() is not the same as getDay()
  • toISOString() uses UTC
  • Use padStart(2, '0') when formatting month/day manually

Quick manual format:

const now = new Date();
const formatted = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;

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 getFullYear().

How do I format the current date as YYYY-MM-DD?

Use this:

const now = new Date();
const formatted = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;

Does toISOString() return local time?

No. It returns the date and time in UTC.

How do I get the current timestamp in JavaScript?

You can use either:

Date.now();

or:

new Date().getTime();
Take Quiz

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

  1. Create a Date object representing the current moment.
  2. Print a human-readable date.
  3. Print the date in YYYY-MM-DD format.
  4. Print the full ISO timestamp.
  5. Use zero padding for month and day in the manual format.
Take Quiz

Keep learning

Related questions

View more