JavaScript Date and Time: The Complete Developer Guide (2026)

JavaScript provides a built-in Date object for working with dates, times, Unix timestamps, and time calculations. Understanding how JavaScript handles UTC, local time, timezones, and date formatting is essential for building reliable web applications and APIs.

In this tutorial, you’ll learn how to get the current date and time, convert Unix timestamps, format dates, work with timezones, calculate date differences, and avoid common JavaScript date and time mistakes.

1. Get the Current Date and Time in JavaScript

You can use the JavaScript Date constructor to get the current date and time.

const now = new Date();

console.log(now);
console.log(now.toString());
console.log(now.toISOString());

The toISOString() method returns a date and time string in ISO 8601 format, expressed in UTC. The output depends on when the code executes.

2. Get the Current Unix Timestamp in JavaScript

The Date.now() method returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC, known as the Unix epoch.


const timestampMs = Date.now();

console.log(timestampMs);

To get a Unix timestamp in seconds, divide the result by 1,000 and round down.

const timestampSeconds = Math.floor(Date.now() / 1000);

console.log(timestampSeconds);

Remember that JavaScript uses milliseconds by default, while many APIs, databases, and Unix timestamp tools use seconds.

Use the Unix Timestamp Converter to convert timestamps into readable dates and times.

3. Convert a Date to a Unix Timestamp

The getTime() method returns the timestamp of a Date object in milliseconds.

const date = new Date("2026-10-30T12:00:00Z");

const milliseconds = date.getTime();
const seconds = Math.floor(milliseconds / 1000);

console.log("Milliseconds:", milliseconds);
console.log("Seconds:", seconds);

Use the appropriate timestamp unit when sending dates to external APIs or storing them in databases.


4. Convert a Unix Timestamp to a JavaScript Date

The JavaScript Date constructor expects a timestamp in milliseconds. If your Unix timestamp is in seconds, multiply it by 1,000.

const timestampSeconds = 1793361600;

const date = new Date(timestampSeconds * 1000);

console.log(date.toISOString());

If the timestamp is already in milliseconds, pass it directly to the Date constructor.

const timestampMilliseconds = 1793361600000;

const date = new Date(timestampMilliseconds);

console.log(date.toISOString());

5. Format Dates in JavaScript

JavaScript provides several methods for formatting dates and times.

Using toLocaleString()

const date = new Date();

console.log(date.toLocaleString());
console.log(date.toLocaleDateString());
console.log(date.toLocaleTimeString());

These methods format the date according to the selected locale and timezone settings.

Using Intl.DateTimeFormat

The Intl.DateTimeFormat API provides more control over date and time formatting.


const date = new Date();

const formatter = new Intl.DateTimeFormat("en-US", {
    year: "numeric",
    month: "long",
    day: "numeric",
    hour: "2-digit",
    minute: "2-digit",
    second: "2-digit"
});

console.log(formatter.format(date));

This approach is useful for displaying readable dates in dashboards, reports, websites, and applications.

6. Convert UTC to Another Timezone

You can use the timeZone option in Intl.DateTimeFormat to display the same instant in different timezones.

const date = new Date("2026-10-30T12:00:00Z");

const newYorkFormatter = new Intl.DateTimeFormat("en-US", {
    dateStyle: "full",
    timeStyle: "long",
    timeZone: "America/New_York"
});

const indiaFormatter = new Intl.DateTimeFormat("en-IN", {
    dateStyle: "full",
    timeStyle: "long",
    timeZone: "Asia/Kolkata"
});

console.log("New York:", newYorkFormatter.format(date));
console.log("India:", indiaFormatter.format(date));

Use IANA timezone identifiers such as America/New_York and Asia/Kolkata instead of hardcoding UTC offsets. Regional timezone identifiers account for applicable daylight saving rules.

Explore the Timezone Converter to compare times across different locations.

7. Parse Date Strings Correctly

You can create a Date object from an ISO 8601 date and time string.


const date = new Date("2026-10-30T15:30:00Z");

console.log(date.toISOString());

When representing a specific instant, use an explicit timezone such as Z for UTC or a numeric UTC offset.

Be Careful with Date-Only Strings

const date = new Date("2026-10-30");

console.log(date.toISOString());

A date-only ISO string is interpreted as midnight UTC. When displayed in a timezone west of UTC, it can appear as the previous calendar day.

Avoid ambiguous strings such as 10/11/2026, because their interpretation may vary depending on the intended date format. Non-standard date strings can also behave differently across JavaScript engines.

8. Add or Subtract Days from a Date

Use the setDate() method to move a date forward or backward by a number of calendar days.

Add Seven Days

const date = new Date();

date.setDate(date.getDate() + 7);

console.log(date.toString());

Subtract Seven Days

const date = new Date();

date.setDate(date.getDate() - 7);

console.log(date.toString());

Calendar-day arithmetic is not always equivalent to adding or subtracting exactly 24 hours, particularly around daylight saving transitions.


9. Calculate the Difference Between Two Dates

Subtracting two valid Date timestamps returns the elapsed time in milliseconds.

const start = new Date("2026-10-01T00:00:00Z");
const end = new Date("2026-10-30T00:00:00Z");

const differenceMs = end.getTime() - start.getTime();
const differenceDays = differenceMs / (1000 * 60 * 60 * 24);

console.log("Difference in milliseconds:", differenceMs);
console.log("Difference in days:", differenceDays);

This example calculates elapsed 24-hour periods. For calendar-day differences, use calendar components in a clearly defined timezone.

10. Get UTC and Local Date Components

JavaScript provides separate methods for accessing local date components and UTC date components.

const date = new Date();

console.log("Local year:", date.getFullYear());
console.log("Local month:", date.getMonth() + 1);
console.log("Local day:", date.getDate());

console.log("UTC year:", date.getUTCFullYear());
console.log("UTC month:", date.getUTCMonth() + 1);
console.log("UTC day:", date.getUTCDate());

The getMonth() and getUTCMonth() methods return values from 0 to 11. January is 0, February is 1, and December is 11.

Use UTC methods when you need calendar components in UTC rather than in the browser’s local timezone.

11. Validate a Date in JavaScript

You can check whether a value produces a valid Date object by testing whether its timestamp is NaN.

function isValidDate(value) {
    const date = new Date(value);

    return !Number.isNaN(date.getTime());
}

console.log(isValidDate("2026-10-30"));
console.log(isValidDate("not-a-date"));

For user input, validate the expected format and calendar values explicitly. The Date constructor may normalize some out-of-range date components instead of rejecting them.

12. Common JavaScript Date and Time Mistakes

Mistake 1: Confusing Seconds with Milliseconds

Unix timestamps in seconds must be multiplied by 1,000 before passing them to the Date constructor.

// Incorrect: interpreted as milliseconds
const incorrectDate = new Date(1793361600);

// Correct: timestamp is in seconds
const correctDate = new Date(1793361600 * 1000);

console.log(correctDate.toISOString());

Mistake 2: Forgetting That Months Start at Zero

The numeric month argument in the multi-argument Date constructor starts at zero.

// Incorrect if you intend October
const incorrectDate = new Date(2026, 10, 30);

// Correct: October is month 9
const correctDate = new Date(2026, 9, 30);

console.log(correctDate.toString());

This constructor uses local time. Use an explicit timezone when you need an unambiguous instant.

Mistake 3: Confusing Local Time with UTC

const date = new Date();

console.log("Local hour:", date.getHours());
console.log("UTC hour:", date.getUTCHours());

The local hour depends on the runtime’s timezone, while the UTC hour is always expressed in UTC.

Mistake 4: Relying on Ambiguous Date Strings

A string such as 10/11/2026 may represent October 11 or November 10. Prefer ISO 8601 strings with explicit timezone information when representing a specific instant.

Mistake 5: Assuming Timezone Offsets Never Change

Regional UTC offsets can change because of daylight saving rules and government decisions. Use IANA timezone identifiers for timezone-aware formatting and calculations rather than assuming that a fixed offset always applies.

13. Frequently Asked Questions

How do I get the current timestamp in JavaScript?

Use Date.now() to get the current timestamp in milliseconds. Use Math.floor(Date.now() / 1000) to get Unix timestamp seconds.

How do I convert a timestamp to a date?

For a Unix timestamp in seconds, multiply it by 1,000 and pass the result to new Date(). If the timestamp is already in milliseconds, pass it directly.

How do I get the current UTC date and time?

Use new Date().toISOString() to obtain the current instant as a UTC ISO 8601 string.

What is the best way to format dates in JavaScript?

Use Intl.DateTimeFormat for locale-aware display and toISOString() for standardized UTC serialization.

Does JavaScript automatically handle daylight saving time?

JavaScript uses timezone rules supplied by the runtime for local date operations and supported IANA timezones. However, date arithmetic and ambiguous local times still require careful handling.

Conclusion

JavaScript’s Date object and Intl.DateTimeFormat API provide the foundation for working with dates and times in web applications. Understanding Unix timestamps, UTC, local time, timezone identifiers, date parsing, and formatting helps prevent common bugs.

For more date and time utilities, visit EpochConvert.com to convert Unix timestamps, format dates, and work with timezones.

Leave a Reply

Your email address will not be published. Required fields are marked *