简体   繁体   English

ASP.Net 核心 MVC FromQuery DateTime in utc

[英]ASP.Net core MVC FromQuery DateTime in utc

I've got some code similar to:我有一些类似的代码:

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

I want to make sure I receive in UTC as follows: If a timezone is specified then convert to UTC If no timezone is specified then assume UTC and specify as UTC我想确保我以 UTC 接收,如下所示:如果指定了时区,则转换为 UTC 如果未指定时区,则假定为 UTC 并指定为 UTC

I tried sending as utc from the client like this (Client works in local time), and it formats as I would expect but unfortunately serverside the datetime was converted to local time of the server.我尝试像这样从客户端以 utc 发送(客户端在本地时间工作),它的格式与我预期的一样,但不幸的是服务器端的日期时间被转换为服务器的本地时间。 If I use ToString("s") the server receives the correct datetime value but unfortunately with DatetimeKind.Unspecified:如果我使用 ToString("s") 服务器会收到正确的日期时间值,但不幸的是 DatetimeKind.Unspecified:

protected string Format(DateTime t)
{
    return t.ToUniversalTime().ToString("o");
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM