如何在 Blazor Server 应用程序中获取时区 ID

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

在我的 Blazor 服务器应用程序中,我可以通过 JS Interop 成功获取当前时区,如下所示:

    window.timeZone = () => {
    var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
    return timezone;
};

这会给我例如欧洲/柏林

在我的 C# 代码中,我需要时区 ID 将给定的 UTC 时间转换为当前时区。

我可以手动这样做:

TimeZoneInfo.ConvertTimeBySystemTimeZoneId(file.CreatedDateTime.Value, "W. Europe Standard Time");

其中“W.Europe Standard Time”是时区 ID。现在我的问题是,当给出时区(例如欧洲/柏林)时,如何动态地找出这个时区 ID?

提前感谢您的帮助。

c# timezone blazor-server-side
1个回答
0
投票

您可以使用 TimeZoneInfo,“Europe/Berlin”是“Id”,
“W. Europe Standard Time”是“StandardName”。

TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin");
Console.WriteLine(timeZone.StandardName);

enter image description here

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.