Introduction
When building applications used by people around the world, handling time zones becomes very important. A date and time displayed in one country may be different in another country because each location follows its own time zone.
JavaScript automatically works with the user’s local time zone but also provides methods for working with UTC (Coordinated Universal Time) and formatting dates for different regions.
For automation engineers, understanding time zones is essential when validating timestamps, testing global applications, verifying server responses, scheduling automated tests, comparing dates from different regions, and working with APIs that return UTC timestamps.
What is a Time Zone?
A time zone is a region of the world that observes the same standard time.
For example:
India follows Indian Standard Time (IST).
The United Kingdom uses Greenwich Mean Time (GMT) or British Summer Time (BST) depending on the season.
Japan follows Japan Standard Time (JST).
The United States has multiple time zones such as Eastern, Central, Mountain, and Pacific.
JavaScript automatically uses the time zone of the user’s computer unless otherwise specified.
Local Time vs UTC
There are two common ways to work with dates in JavaScript:
Local Time
Uses the user’s system time zone.
Example:
const now = new Date();
console.log(now.toLocaleString());
Sample Output
22/6/2026, 4:45:18 PM
UTC Time
Uses Coordinated Universal Time (UTC).
const now = new Date();
console.log(now.toUTCString());
Sample Output
Mon, 22 Jun 2026 11:15:18 GMT
Example 1: Display Local Date and Time
const today = new Date();
console.log(today.toLocaleString());
Sample Output
22/6/2026, 4:45:18 PM
Example 2: Display UTC Date and Time
const today = new Date();
console.log(today.toUTCString());
Sample Output
Mon, 22 Jun 2026 11:15:18 GMT
Example 3: Display ISO Date
const today = new Date();
console.log(today.toISOString());
Sample Output
2026-06-22T11:15:18.456Z
The Z at the end indicates UTC (Zulu Time).
Example 4: Get the Time Zone Offset
const today = new Date();
console.log(today.getTimezoneOffset());
Sample Output
-330
For India, the offset is typically -330 minutes, meaning IST is UTC+5:30.
Example 5: Display Date for Another Locale
const today = new Date();
console.log(today.toLocaleString("en-US"));
Sample Output
6/22/2026, 4:45:18 PM
Example 6: Display Date in a Specific Time Zone
const today = new Date();
console.log(
today.toLocaleString("en-US", {
timeZone: "Asia/Tokyo"
})
);
Sample Output
6/22/2026, 8:15:18 PM
The displayed time depends on the specified time zone.
Common Time Zone Methods
| Method | Description |
|---|---|
toLocaleString() | Displays date and time in the local format |
toLocaleDateString() | Displays the local date |
toLocaleTimeString() | Displays the local time |
toUTCString() | Displays the date and time in UTC |
toISOString() | Returns the date in ISO 8601 format (UTC) |
getTimezoneOffset() | Returns the difference between local time and UTC in minutes |
Real-World Example
Display both local and UTC times.
const now = new Date();
console.log("Local:", now.toLocaleString());
console.log("UTC:", now.toUTCString());
Sample Output
Local: 22/6/2026, 4:45:18 PM
UTC: Mon, 22 Jun 2026 11:15:18 GMT
Another example:
Display the time in Japan.
const now = new Date();
console.log(
now.toLocaleString("en-US", {
timeZone: "Asia/Tokyo"
})
);
Sample Output
6/22/2026, 8:15:18 PM
Automation Testing Example
Time zones are important when testing applications used across multiple countries.
Playwright Example
Verify a UTC timestamp returned by an application.
const timestamp = new Date().toISOString();
console.log(timestamp);
Sample Output
2026-06-22T11:15:18.456Z
Selenium Example
Compare local and UTC times.
const now = new Date();
console.log(now.toLocaleString());
console.log(now.toUTCString());
Cypress Example
Check the user’s local time zone.
const offset = new Date().getTimezoneOffset();
console.log(offset);
Sample Output
-330
API Testing Example
Validate an API timestamp.
const apiTime = new Date().toISOString();
console.log(apiTime);
Sample Output
2026-06-22T11:15:18.456Z
Data-Driven Testing Example
Generate a report using UTC time.
const reportTime = new Date().toUTCString();
console.log(reportTime);
Sample Output
Mon, 22 Jun 2026 11:15:18 GMT
Common Mistakes
Confusing Local Time with UTC
console.log(new Date().toLocaleString());
console.log(new Date().toISOString());
These methods produce different outputs because one uses the local time zone while the other uses UTC.
Assuming Everyone Has the Same Time Zone
Applications used worldwide must account for users in different time zones.
Avoid assuming all users share the same local time.
Ignoring Daylight Saving Time (DST)
Some countries adjust their clocks during part of the year.
JavaScript automatically applies Daylight Saving Time when the selected time zone observes it.
Best Practices
Store timestamps in UTC whenever possible.
Convert dates to the user’s local time before displaying them.
Use
toISOString()for APIs and databases.Use
toLocaleString()for user-friendly displays.Test applications with users in different time zones.
Be aware of Daylight Saving Time changes.
Avoid hardcoding time zone offsets.
Conclusion
Time zones are an important aspect of working with dates and times in JavaScript. The Date object provides built-in methods for working with both local time and UTC, making it easier to build applications that work correctly across different regions.
For automation engineers, understanding time zones is essential for validating timestamps, comparing server and client times, testing international applications, and ensuring consistent behavior across different geographic locations.
Mastering time zones helps you build reliable, globally accessible JavaScript applications.
Frequently Asked Questions (FAQs)
What is a time zone?
A time zone is a region that follows a specific standard time.
What is UTC?
UTC (Coordinated Universal Time) is the international time standard used worldwide.
How do I display the current time in UTC?
Use:
const now = new Date();
console.log(now.toUTCString());
What does toISOString() return?
It returns the date and time in the ISO 8601 format using UTC.
What does getTimezoneOffset() return?
It returns the difference, in minutes, between the local time zone and UTC.
Why are time zones important in automation testing?
Automation engineers use time zones to validate timestamps, compare server and client times, test global applications, verify API responses, and ensure correct date handling across different regions.
Key Takeaways
A time zone defines the standard time for a geographic region.
JavaScript uses the user’s local time zone by default.
UTC is the global standard for time.
Use
toLocaleString()for displaying local dates and times.Use
toUTCString()andtoISOString()for UTC-based values.getTimezoneOffset()returns the local offset from UTC.Store timestamps in UTC for consistency.
Convert UTC values to local time before displaying them to users.
Time zones are critical in Playwright, Selenium, Cypress, API testing, and global JavaScript applications.
Understanding time zones helps build reliable applications for users around the world.
