c# - Blazor 服务器端 : How can I read out the local time of the user/browser not of the server (using only c#)

标签 c# server localization utc blazor-server-side

我创建了一个页面,其中为用户显示天气数据(我通过 api 调用获取数据)。我有来自 api 调用的数据的 UTC 时间戳。
我的问题是,如果我通过 ToLocalTime() 转换 UTC 时间,则显示的是我服务器的本地时间而不是用户浏览器的本地时间。
有没有办法只使用 C# 和 Blazor 向用户显示“他们的”时间:UTCTime.ToUserTimeZone()?
我对 JS 等零知识,或者这是唯一的方法。我用谷歌搜索并找到了很多答案,我必须实现 JavaScript 来“获取”用户本地化。
感谢您的帮助和时间

最佳答案

没有javascript就不可能获得用户时间。
如果使用 Blazor 服务器端,则 C# 代码在服务器上运行。
但是你可以调用javascript函数。
https://docs.microsoft.com/en-us/aspnet/core/blazor/call-javascript-from-dotnet?view=aspnetcore-3.1
这是一个用于获取用户日期和时间偏移的 javascript(在 _Host.cshtml 的标题元素中添加脚本标记):

<script>
    window.localDate = () => {
        var ldCurrentDate = new Date();
        return ldCurrentDate.getFullYear() +
            "-" + String(ldCurrentDate.getMonth() + 1).padStart(2, '0') +
            "-" + String(ldCurrentDate.getDate()).padStart(2, '0') +
            "T" +
            String(ldCurrentDate.getHours()).padStart(2, '0') +
            ":" + String(ldCurrentDate.getMinutes()).padStart(2, '0') +
            ":" + String(ldCurrentDate.getSeconds()).padStart(2, '0');
    };
    window.utcDate = () => {
        var ldCurrentDate = new Date();
        return ldCurrentDate.getUTCFullYear() +
            "-" + String(ldCurrentDate.getUTCMonth() + 1).padStart(2, '0') +
            "-" + String(ldCurrentDate.getUTCDate()).padStart(2, '0') +
            "T" +
            String(ldCurrentDate.getUTCHours()).padStart(2, '0') +
            ":" + String(ldCurrentDate.getUTCMinutes()).padStart(2, '0') +
            ":" + String(ldCurrentDate.getUTCSeconds()).padStart(2, '0');
    };
    window.timeZoneOffset = () => {
        return new Date().getTimezoneOffset() / 60;
    };
</script>
从 C# 调用:
@page "/dates"
@inject IJSRuntime JSRuntime;

<button type="button" @onclick="GetDates">
    Get Dates
</button>

<p>
    <span>
        User Date: @string.Format("{0:G}", this.UserDate)
    </span>
</p>
<p>
    <span>
        Utc Date: @string.Format("{0:G}", this.UTCDate)
    </span>
</p>
<p>
    <span>
        TimeZoneOffset: @string.Format("{0}", this.TimeZoneOffset)
    </span>
</p>
<p>
    <span>
        ServerDate: @string.Format("{0:G}", this.ServerDate)
    </span>
</p>


@code {

    private DateTime UserDate { get; set; }
    private DateTime UTCDate { get; set; }
    private DateTime ServerDate { get; set; }
    private int TimeZoneOffset { get; set; }


    private async Task GetDates()
    {
        this.ServerDate = DateTime.Now;
        this.UserDate = await JSRuntime.InvokeAsync<DateTime>("localDate");
        this.UTCDate = await JSRuntime.InvokeAsync<DateTime>("utcDate");
        this.TimeZoneOffset = await JSRuntime.InvokeAsync<int>("timeZoneOffset");
    }
}

关于c# - Blazor 服务器端 : How can I read out the local time of the user/browser not of the server (using only c#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63599853/

相关文章:

wordpress - 如何本地化联系表 7 中的字段标签

mysql - 选择多篇翻译文章(记录)

localization - 如果您有一个应用程序本地化为 pt-br 和 pt-pt,如果系统只报告 "pt"代码,您应该选择哪种语言?

c# - Bash 命令未通过 .net 核心进程执行

c# - LINQ To SQL 不提交更改

http - 将 Go 网络服务器部署到 Google 计算引擎

node.js - 在 Node.js Express 中禁用 TLS 1.0 和 1.1 或仅使用 TLS 1.2 及更高版本

node.js - 远程访问个人Web服务器

c# - 在列表上使用.Where()

c# - 到特定客户端的 TCP C# 数据包