Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Display the Asian and American Date Time with Date Object in JavaScript
JavaScript's Date object provides powerful timezone handling through the toLocaleString() method. You can display dates and times for different regions by specifying timezone identifiers.
Syntax
new Date().toLocaleString("en-US", {timeZone: "timezone_identifier"});
Parameters
- locale: Language and region format (e.g., "en-US", "en-GB")
- timeZone: IANA timezone identifier (e.g., "Asia/Kolkata", "America/New_York")
Asian Time Zone Example
// Display current time in Asian timezone (India)
var asianDateTime = new Date().toLocaleString("en-US", {
timeZone: "Asia/Kolkata"
});
console.log("Asian Date Time (India):");
console.log(asianDateTime);
// Convert to Date object for further processing
var asianDateObject = new Date(asianDateTime);
console.log("As Date Object:", asianDateObject);
Asian Date Time (India): 12/15/2023, 2:30:45 PM As Date Object: 2023-12-15T09:00:45.000Z
American Time Zone Example
// Display current time in American timezone (New York)
var americanDateTime = new Date().toLocaleString("en-US", {
timeZone: "America/New_York"
});
console.log("American Date Time (New York):");
console.log(americanDateTime);
// Convert to Date object for further processing
var americanDateObject = new Date(americanDateTime);
console.log("As Date Object:", americanDateObject);
American Date Time (New York): 12/15/2023, 4:00:45 AM As Date Object: 2023-12-15T09:00:45.000Z
Complete Example - Both Timezones
// Get current time in both Asian and American timezones
var asianTime = new Date().toLocaleString("en-US", {
timeZone: "Asia/Kolkata"
});
var americanTime = new Date().toLocaleString("en-US", {
timeZone: "America/New_York"
});
console.log("=== Timezone Comparison ===");
console.log("Asian Time (Kolkata):", asianTime);
console.log("American Time (New York):", americanTime);
// Calculate time difference
var asianDate = new Date(asianTime);
var americanDate = new Date(americanTime);
var timeDifference = Math.abs(asianDate - americanDate) / (1000 * 60 * 60);
console.log("Time Difference:", timeDifference, "hours");
=== Timezone Comparison === Asian Time (Kolkata): 12/15/2023, 2:30:45 PM American Time (New York): 12/15/2023, 4:00:45 AM Time Difference: 10.5 hours
Common Timezone Identifiers
| Region | Timezone Identifier | Description |
|---|---|---|
| Asia | Asia/Kolkata | India Standard Time |
| Asia | Asia/Tokyo | Japan Standard Time |
| America | America/New_York | Eastern Time |
| America | America/Los_Angeles | Pacific Time |
Key Points
- Use IANA timezone identifiers for accurate timezone conversion
- The
toLocaleString()method returns a formatted string, not a Date object - Convert back to Date object using
new Date()if needed for calculations - Timezone identifiers are case-sensitive
Conclusion
JavaScript's toLocaleString() with timezone options provides an easy way to display dates across different regions. Use IANA timezone identifiers for accurate conversions between Asian and American time zones.
Advertisements
