JavaScript has a new Date() constructor which is used to create a date object to get the current date and time. This date object uses the UTC timezone or the client browser’s timezone, i.e. if you are in India and use the new Date() constructor to get the date and time, you will get your local time. But sometimes, we may need to get the timezone of another country, which we can’t do directly. These can be done using the toLocaleString() method or the format() method. By the end of the article, you will be able to get the date of any other timezone in JavaScript.
The two methods that we will use in this article to convert a date to another time zone are as follows −
-
Using toLocaleString() Method
-
使用format()方法
使用 toLocaleString() 方法
toLocaleString() 方法可以使用日期对象调用。该方法具有根据传入的参数将数据从一个时区转换为另一个时区的能力。它接受两个参数,第一个参数是“locale”,它是应该使用的格式约定的语言,对于英语来说是“en-US”,第二个参数是“options”,对于我们来说是{timeZone:“countryName”},其中countryName是我们想要更改时区的国家的名称。
以下是使用toLocaleString()方法在JavaScript中将日期转换为另一个时区的逐步过程。
-
使用Date构造函数创建一个日期对象
-
Use the date object with toLocaleString() method and pass the first argument as ‘en-US’ for English language date and time formatting, and the second argument {timeZone: “America/New_York”} for getting the timezone of New York
-
Store the value return from this method into a variable, that variable is our required timezone.
Example
在这个例子中,我们使用JavaScript的toLocaleString()方法将一个日期转换为另一个时区。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Converting date to another timezone in JavaScript</title>
</head>
<body>
<h3>Convert date to America/New_York Time Zone using toLocaleString() Method</h3>
<p id="input">Local Time: </p>
<p id="output">America/New_York Time Zone: </p>
<script>
// date objec
let date = new Date();
document.getElementById("input").innerText += date ;
// convert date to another timezone
let output = date.toLocaleString("en-US", {
timeZone: "America/New_York"
});
// display the result
document.getElementById("output").innerText += output;
</script>
</body>
</html>
Using Format() Method
We can use the format() method with the “Intl.DateTimeFormat” object and use the date object passed as an argument to the format() method to convert the timezone to a timezone passed while creating the “Intl.DateTimeFormat” object. It sounds complicated but it is very simple if you look at the example below.
Here is the step-wise procedure to convert a date to another timezone in JavaScript using format() Method.
-
Create a date object using the Date constructor.
-
在创建“Intl.DateTimeFormat”对象时,将第一个参数设置为’en-US’以进行英语语言的日期和时间格式化,第二个参数{timeZone: “America/New_York”}用于获取纽约的时区。
-
Use the format() method with this object and pass the date object as an argument and store it in a variable, that variable is our required timezone.
Example
In this example, we are converting a date to another timezone in JavaScript using the format() Method.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Convert date to America/New_York timezone in JavaScript</title>
</head>
<body>
<h3>Convert date to America/New_York timezone using format() Method</h3>
<p id="input">Local Time: </p>
<p id="output">America/New_York Time Zone: </p>
<script>
// date objec
let date = new Date();
document.getElementById("input").innerText += date ;
// create a new date object
let newObj = Intl.DateTimeFormat('en-US', {
timeZone: "America/New_York"
})
// convert date to another timezone
let newDate = newObj.format(date);
// display the result
document.getElementById("output").innerHTML += newDate;
</script>
</body>
</html>
Summary
让我们总结一下在本教程中学到的内容。我们看到我们有两种方法可以将日期转换为另一个时区,第一种是使用日期对象的toLocaleString()方法,第二种是使用”Intl.DateTimeFormat”对象的format()方法。这两种方法有不同的用例,你可以根据自己的需要选择。我们推荐使用toLocaleString()方法,它易于使用,而且比使用”Intl.DateTimeFormat”对象的format()方法需要更少的代码行。
以上就是如何在 JavaScript 中将日期转换为另一个时区?的详细内容,更多请关注双恒网络其它相关文章!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
云资源网 » 如何在 JavaScript 中将日期转换为另一个时区?
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 你们有qq群吗怎么加入?