How to use toLocaleString() method In Javascript
The toLocaleString() method is used to return a string that formats the date according to the locale and options specified. It will convert the date on which the method is used from one timezone to another.
Syntax:
usaTime = date.toLocaleString("en-US", {timeZone: "America/New_York"});
Example: In this example, we will use Using toLocaleString() method
<!DOCTYPE html>
<html lang="en">
<head>
<title>
How to convert date to another timezone in JavaScript ?
</title>
</head>
<body>
<h1 style="color: green">
w3wiki
</h1>
<b>
How to convert date to another
timezone in JavaScript?
</b>
<br><br>
<button onclick="changeTimezone()">
Change timezone
</button>
<script type="text/javascript">
function changeTimezone() {
let date =
new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
document.write('Given IST datetime: ' + date + "<br>");
let usaTime =
date.toLocaleString("en-US", {
timeZone: "America/New_York"
});
document.write('USA datetime: ' + usaTime);
}
</script>
</body>
</html>
Output:
How to convert date to another timezone in JavaScript ?
In our globalized world, navigating time zones has become an essential aspect of modern life. Whether it’s scheduling international meetings, coordinating travel plans, or simply staying in touch with friends and family abroad, understanding how to convert dates and times between different time zones is invaluable. In this article, we will learn how to convert the date of one timezone to another.
Below are the methods to convert date to another timezone in JavaScript:
Table of Content
- Using Intl.DateTimeFormat() and format()
- Using toLocaleString() method