gpt4 book ai didi

c# - ASP.Net 核心 MVC FromQuery DateTime in utc

转载 作者:行者123 更新时间:2023-12-04 13:46:17 24 4
gpt4 key购买 nike

我有一些类似的代码:

 [HttpGet("SearchStuff")]
public IActionResult SearchStuff([FromQuery]DateTime from, [FromQuery]DateTime to)

我想确保我在 UTC 中收到如下:
如果指定了时区,则转换为 UTC
如果未指定时区,则假定为 UTC 并指定为 UTC

我尝试像这样从客户端以 utc 发送(客户端在本地时间工作),它的格式与我预期的一样,但不幸的是服务器端的日期时间被转换为服务器的本地时间。如果我使用 ToString("s") 服务器会收到正确的日期时间值,但不幸的是 DatetimeKind.Unspecified:
protected string Format(DateTime t)
{
return t.ToUniversalTime().ToString("o");
}

最佳答案

还有这个https://github.com/dotnet/aspnetcore/issues/11584这提供了一种解决方法

关于c# - ASP.Net 核心 MVC FromQuery DateTime in utc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47474103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com