与MomentJs一起使用.NET Core TimeZoneInfo的最佳实践是什么?

发布于 2025-02-03 12:06:26 字数 312 浏览 3 评论 0原文

我们根据.NETCORE TIMEZONEINFO生成的列表存储用户的时区。从那里开始,我们允许最终用户使用拾取器来指定日期和时间,这些选择器存储在数据库中。所有这些都很好。用户知道时区指定“在哪里”,从而选择了一个日期和时间。

但是,在前端,我们使用MomentJS进行格式化。由于我知道timeZoneInfo ID也在前端上是什么,所以我想知道是否可以将日期和timeZoneID插入MomentJS中,以使用.format()命令来操纵日期。

我正在考虑放弃时刻,并格式化后端上的日期,但是MomentJs为我们的UI设计师提供了灵活性而不必重新部署。

想法?

We store the user's timezone based on a list generated by the .netcore TimeZoneInfo. From there, we allow the end user to specify dates and times using pickers, which are stored to the database. All of this is working fine. User picks a date and time, knowing that the timezone specifies "where".

On the frontend however, we use momentJS for formatting. Since I know what the timezoneinfo Id is on the frontend as well, I was wondering if I can plug the date and timezoneid into momentjs to use the .format() command to manipulate dates.

I'm considering ditching momentjs, and formatting the dates on the backend, but momentjs affords our UI designers flexibility without having to redeploy.

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

上课铃就是安魂曲 2025-02-10 12:06:26

一些事情:

  • 您确实应该考虑用更现代的图书馆代替时刻。请阅读 moment project project status (我写的)(我写了)以获取更多详细信息。<<<<<<<<<<<<<<<< /p>

    • 如果您 将继续使用时刻,您还需要时刻timezone 附加库与时区合作。如果您选择一个库,例如 luxon 作为替换,时区支持是内置的。

    • 另外,您可以仅考虑使用 date.toleocalestring 使用timezone选项。如果满足您的需求,那么您根本就不需要图书馆。

  • timeZoneInfo .net中的对象将其id属性与正在运行的平台对齐。也就是说,您在Windows上获得Windows ID,以及Linux和MacOS上的IANA ID。如果您不熟悉差异,则应阅读 timezone tag wiki 。 P>

  • Windows时区域ID对JavaScript库无用。您将始终需要使用IANA ID。

    • 如果您要在Linux上运行服务器,则在timezoneinfo.id属性中已经有IANA值,并且已设置。您可以直接在JavaScript中使用它们。

    • 如果您要在Windows上运行服务器,并且使用.NET 6或更新的服务器,并且正在使用 ICU安装,您可以使用 timezoneinfo.tryconvertwindowsidtoianaid 从Windows ID获取所需的IANA ID。您可以在我为Microsoft写的博客文章

    • 如果您在Windows上运行并且在.NET 5或更低的上方运行,或者在未安装ICU的旧Windows上,您可以使用我的 timezoneconverter 库在Windows和IANA ID之间转换。

A few things:

  • You really should consider replacing Moment with a more modern library. Please read the Moment Project Status (that I wrote) for further details.

    • If you were going to continue using Moment, you would also need the Moment-Timezone add-on library to work with time zones. If you choose a library such as Luxon as a replacement, time zone support is built-in.

    • Alternatively, You might just consider using Date.toLocaleString with the timeZone option. If that meets your needs, then you won't need a library at all.

  • TimeZoneInfo objects in .NET align their Id property with the platform they are running on. That is, you get Windows IDs on Windows, and IANA IDs on Linux and macOS. If you're unfamiliar with the difference, you should read the timezone tag wiki.

  • Windows time zone IDs are not useful with JavaScript libraries. You will always need to use IANA IDs.

    • If you are running your server on Linux, you already have IANA values in your TimeZoneInfo.Id property, and are all set. You can just use them directly in JavaScript.

    • If you are running your server on Windows, and are using .NET 6 or newer and are on a system with ICU installed, you can use TimeZoneInfo.TryConvertWindowsIdToIanaId to get the required IANA ID from a Windows ID. You can read more about this in the blog post I wrote for Microsoft.

    • If you are running on Windows and are on .NET 5 or lower, or are on older Windows without ICU installed, you can use my TimeZoneConverter library to convert between Windows and IANA IDs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文