Introduction
In many JavaScript applications, it is important to know the current date and time. Whether you are displaying today’s date, showing the current time, recording user activities, generating timestamps, scheduling events, or measuring execution time, JavaScript makes this easy with the Date object.
The Date object retrieves the current date and time from the user’s system clock. Once a Date object is created, you can display it in different formats or extract individual components such as the year, month, day, hour, minute, and second.
For automation engineers, retrieving the current date and time is useful for creating unique test data, generating report names, validating timestamps, measuring test execution duration, logging API requests, and scheduling automated tasks.
What is the Current Date and Time?
The current date and time represent the exact date and time on the user’s system when the JavaScript code executes.
JavaScript retrieves this information using the built-in Date object.
Creating the Current Date and Time
To get the current date and time, create a new Date object without passing any arguments.
Syntax
const currentDate = new Date();
The Date object automatically contains the current system date and time.
Example 1: Display Current Date and Time
const currentDate = new Date();
console.log(currentDate);
Sample Output
Mon Jun 22 2026 16:45:18 GMT+0530 (India Standard Time)
Note: The output depends on the user’s system date, time, and time zone.
Example 2: Display Only the Current Date
const currentDate = new Date();
console.log(currentDate.toDateString());
Sample Output
Mon Jun 22 2026
Example 3: Display Only the Current Time
const currentDate = new Date();
console.log(currentDate.toLocaleTimeString());
Sample Output
4:45:18 PM
The exact format depends on the user’s locale.
Example 4: Display Local Date
const currentDate = new Date();
console.log(currentDate.toLocaleDateString());
Sample Output
22/6/2026
The displayed format varies based on regional settings.
Example 5: Display Local Date and Time
const currentDate = new Date();
console.log(currentDate.toLocaleString());
Sample Output
22/6/2026, 4:45:18 PM
Example 6: Get the Current Timestamp
console.log(Date.now());
Sample Output
1782119718456
The value changes every millisecond.
Methods for Displaying Current Date and Time
| Method | Description |
|---|---|
new Date() | Creates a Date object with the current date and time |
toDateString() | Displays only the date |
toTimeString() | Displays only the time |
toLocaleDateString() | Displays the date in the user’s local format |
toLocaleTimeString() | Displays the time in the user’s local format |
toLocaleString() | Displays both the local date and time |
toISOString() | Displays the date and time in ISO 8601 format |
Real-World Example
Display the current date and time on a webpage.
const now = new Date();
console.log("Current Date:", now.toDateString());
console.log("Current Time:", now.toLocaleTimeString());
Sample Output
Current Date: Mon Jun 22 2026
Current Time: 4:45:18 PM
Another example:
Generate a unique report filename.
const reportName = "Report_" + Date.now() + ".html";
console.log(reportName);
Sample Output
Report_1782119718456.html
Automation Testing Example
Current date and time are widely used in automation testing for reports, logs, screenshots, and unique test data.
Playwright Example
Generate a screenshot filename.
const screenshot = "HomePage_" + Date.now() + ".png";
console.log(screenshot);
Sample Output
HomePage_1782119718456.png
Selenium Example
Record test start time.
const startTime = new Date();
console.log(startTime.toLocaleString());
Sample Output
22/6/2026, 4:45:18 PM
Cypress Example
Log the current execution time.
const executionTime = new Date();
console.log(executionTime.toLocaleTimeString());
Sample Output
4:45:18 PM
API Testing Example
Record the API request timestamp.
const requestTimestamp = new Date().toISOString();
console.log(requestTimestamp);
Sample Output
2026-06-22T11:15:18.456Z
Data-Driven Testing Example
Generate a unique user ID.
const userId = "USER_" + Date.now();
console.log(userId);
Sample Output
USER_1782119718456
Common Mistakes
Forgetting the new Keyword
Incorrect:
const today = Date();
This returns the current date as a string instead of a Date object.
Correct:
const today = new Date();
Expecting the Same Format Everywhere
Different browsers, operating systems, and regional settings may display dates differently.
Use methods such as toLocaleDateString() or toISOString() when a specific format is required.
Using Local Time Instead of UTC
For APIs, databases, and distributed applications, use:
new Date().toISOString();
instead of relying on the local system time.
Best Practices
Always create the current date using
new Date().Use
toLocaleDateString()andtoLocaleTimeString()for user-friendly displays.Use
toISOString()for APIs, databases, and logging.Use
Date.now()when generating unique identifiers.Be aware of time zone differences.
Test applications in different locales when working with dates and times.
Avoid hardcoding dates unless necessary.
Conclusion
The JavaScript Date object provides a simple and efficient way to retrieve the current date and time. It offers multiple formatting methods to display dates in user-friendly or standardized formats, making it suitable for both everyday applications and enterprise systems.
For automation engineers, the current date and time are essential for generating timestamps, naming reports, validating time-sensitive functionality, logging execution details, and creating unique test data.
Understanding how to work with the current date and time is a fundamental skill for every JavaScript developer.
Frequently Asked Questions (FAQs)
How do I get the current date and time in JavaScript?
Use:
const currentDate = new Date();
How do I display only today’s date?
Use:
currentDate.toDateString();
How do I display only the current time?
Use:
currentDate.toLocaleTimeString();
What is Date.now()?
It returns the current timestamp in milliseconds since January 1, 1970 (UTC).
Why does toISOString() display a different time?
toISOString() always returns the date and time in UTC, which may differ from your local time zone.
Why is the current date and time important in automation testing?
Automation engineers use the current date and time to generate timestamps, create unique report names, validate date fields, log execution details, and produce unique test data.
Key Takeaways
Use
new Date()to retrieve the current date and time.toDateString()displays only the date.toLocaleTimeString()displays the local time.toLocaleString()displays both the local date and time.toISOString()returns the date in UTC format.Date.now()returns the current timestamp in milliseconds.Display formats depend on the user’s locale and system settings.
Always consider time zones when working with dates.
Current date and time are widely used in Playwright, Selenium, Cypress, API testing, and Node.js applications.
Mastering current date and time operations is essential for professional JavaScript development.
