从nodeJS或momentJS中的日期获取时区信息

问题描述 投票:0回答:1

我们有一个名为 11:10 PM 的当前时间作为字符串值。我们将用这个时间创建一个日期对象。现在从这个日期我们可以得到这个日期值的时区吗?

我们期望的是 Asia/Kolkata 等时区。是否可以在 NodeJS/MomentJS 中获取这个值

javascript node.js momentjs moment-timezone
1个回答
0
投票

您可以使用 Intl.DateTime.resolvedOptions

获取运行代码的机器的时区

如果你在 Node.js 中运行它,你将获得服务器时区,任何在浏览器中运行代码的客户端都会看到他们的时区。

// Using Intl
console.log('Timezone: ', Intl.DateTimeFormat().resolvedOptions().timeZone);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.30.1/moment.min.js" integrity="sha512-hUhvpC5f8cgc04OZb55j0KNGh4eh7dLxd/dPSJ5VyzqDWxsayYbojWyl5Tkcgrmb/RVKCRJI1jNlRbVP4WWC4w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

© www.soinside.com 2019 - 2024. All rights reserved.