Date Object

Introduction

Dates and times play an essential role in almost every JavaScript application. Whether you’re displaying the current date, scheduling events, calculating ages, generating timestamps, or measuring execution time, JavaScript provides the powerful Date object to handle all date and time operations.

The Date object is a built-in JavaScript object that allows developers to create, retrieve, modify, compare, and format dates and times. It supports both local time and Coordinated Universal Time (UTC), making it suitable for applications used across different time zones.

For automation engineers, the Date object is invaluable for generating timestamps, creating unique test data, validating date fields, measuring execution times, scheduling automated tasks, and naming reports.


What is the Date Object?

The Date object is a built-in JavaScript object used to represent dates and times.

It allows developers to:

  • Create dates

  • Get the current date and time

  • Retrieve individual date components

  • Modify dates and times

  • Compare dates

  • Format dates

  • Perform date calculations


Why Use the Date Object?

The Date object provides several advantages:

  • Handles date and time operations easily

  • Supports local and UTC time

  • Provides built-in formatting methods

  • Simplifies date calculations

  • Generates timestamps

  • Useful for scheduling and logging

  • Widely supported in all modern browsers


Creating a Date Object

The most common way to create a Date object is:

const today = new Date();

console.log(today);

Sample Output

Mon Jun 22 2026 16:45:18 GMT+0530 (India Standard Time)

The exact output depends on the current system date and time.


Common Operations with the Date Object

The Date object supports many operations.

Some of the most commonly used ones include:

  • Getting the current date

  • Getting the current time

  • Creating custom dates

  • Formatting dates

  • Retrieving the year, month, and day

  • Comparing dates

  • Calculating time differences

  • Generating timestamps


Common Date Methods

MethodDescription
new Date()Creates a new Date object
Date.now()Returns the current timestamp in milliseconds
getFullYear()Returns the current year
getMonth()Returns the month (0–11)
getDate()Returns the day of the month
getDay()Returns the day of the week
getHours()Returns the hour
getMinutes()Returns the minutes
getSeconds()Returns the seconds
getMilliseconds()Returns the milliseconds
toDateString()Returns a readable date
toTimeString()Returns a readable time
toLocaleDateString()Returns the local date format
toLocaleTimeString()Returns the local time format
toISOString()Returns the date in ISO 8601 format

Example 1: Get Current Date and Time

const now = new Date();

console.log(now);

Sample Output

Mon Jun 22 2026 16:45:18 GMT+0530

Example 2: Display Only the Date

const today = new Date();

console.log(today.toDateString());

Sample Output

Mon Jun 22 2026

Example 3: Display Only the Time

const now = new Date();

console.log(now.toLocaleTimeString());

Sample Output

4:45:18 PM

Example 4: Get the Current Year

const today = new Date();

console.log(today.getFullYear());

Sample Output

2026

Example 5: Get the Current Month

const today = new Date();

console.log(today.getMonth());

Sample Output

5

Note: Months are zero-based (0 = January, 5 = June).


Example 6: Get a Timestamp

console.log(Date.now());

Sample Output

1782119718456

The value changes every millisecond.


Real-World Example

Display the current local date and time.

const now = new Date();

console.log(now.toLocaleString());

Sample Output

22/6/2026, 4:45:18 PM

Another example:

Generate a unique filename.

const fileName = "Report_" + Date.now() + ".pdf";

console.log(fileName);

Sample Output

Report_1782119718456.pdf

Automation Testing Example

The Date object is extensively used in automation frameworks.

Playwright Example

Generate a timestamp for screenshots.

const screenshotName = "HomePage_" + Date.now() + ".png";

console.log(screenshotName);

Selenium Example

Measure test execution time.

const startTime = new Date();

console.log(startTime);

Cypress Example

Log the current execution time.

const executionTime = new Date();

console.log(executionTime.toLocaleTimeString());

API Testing Example

Store the request timestamp.

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

console.log(requestTime);

Data-Driven Testing Example

Generate unique usernames.

const username = "user" + Date.now();

console.log(username);

Topics Covered in This Section

In the following lessons, you’ll learn:

  • Current Date and Time

  • Creating Custom Dates

  • Getting Date Components

  • Setting Date Components

  • Formatting Dates

  • UTC Date Methods

  • Comparing Dates

  • Calculating Date Differences

  • Working with Timestamps

  • Practical Date Examples


Common Mistakes

Forgetting the new Keyword

Incorrect:

const today = Date();

Correct:

const today = new Date();

Forgetting That Months Start at Zero

new Date(2026, 5, 22);

This represents June 22, 2026, not May.


Confusing getDay() with getDate()

  • getDay() → Day of the week (06)

  • getDate() → Day of the month (131)


Best Practices

  • Always create dates using new Date().

  • Use toISOString() for APIs and logging.

  • Use toLocaleDateString() for displaying dates to users.

  • Remember that months are zero-based.

  • Use Date.now() for timestamps and unique identifiers.

  • Test date-related functionality across different time zones if applicable.

  • Avoid hardcoding dates unless required.


Conclusion

The JavaScript Date object is a powerful built-in feature for handling dates and times. It enables developers to create dates, retrieve date components, format values, calculate differences, and generate timestamps with ease.

For automation engineers, mastering the Date object is essential for creating reports, logging execution times, validating application dates, generating unique test data, and building reliable automation frameworks.

Understanding the Date object provides a strong foundation for working with time-based features in modern JavaScript applications.


Frequently Asked Questions (FAQs)

What is the JavaScript Date object?

It is a built-in object used to create and manipulate dates and times.


How do I create the current date?

const today = new Date();

Why does getMonth() return 5 for June?

Because JavaScript counts months from 0 (January) to 11 (December).


What does Date.now() return?

It returns the current timestamp in milliseconds since January 1, 1970 (UTC).


What is toISOString() used for?

It returns the date and time in ISO 8601 format, which is commonly used in APIs and databases.


Why is the Date object important in automation testing?

It is used for timestamps, execution logs, report names, unique test data, date validations, and measuring test durations.


Key Takeaways

  • The Date object is used to work with dates and times.

  • Create a date using new Date().

  • Use built-in methods to retrieve and format date information.

  • Date.now() returns the current timestamp.

  • Months are zero-based in JavaScript.

  • Use toISOString() for APIs and logging.

  • Use locale methods for user-friendly display.

  • The Date object supports date calculations and comparisons.

  • It is widely used in Playwright, Selenium, Cypress, API testing, and Node.js applications.

  • Mastering the Date object is essential for professional JavaScript development.