DateTimeOffset modelbinding from form is always set to utc in net7
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Describe the bug
Hello,
I have the following model that is used as input of a controller method:
public class TestController : ControllerBase
{
[HttpPost]
public DateTimeOffset Post([FromForm] InputModel model)
{
return model.StartDate;
}
}
public class InputModel
{
public DateTimeOffset StartDate { get; set; }
}
When I sent the string ‘2022-11-29 10:00’ in NET6, the local timezone offset (+1 in my case) is used for the DateTimeOffset. However, in NET7, the offset is always 0.
Expected Behavior
I expect the datetimeoffset is parsed with the local timezone offset, if none is provided in the input string.
Steps To Reproduce
https://github.com/yannicsmeets/datetimeoffset-demo
The main branch is net6. In the net7 branch, the target framework is set to net7.0
Exceptions (if any)
No response
.NET Version
7.0.100
Anything else?
No response
Issue Analytics
- State:
- Created 10 months ago
- Comments:10 (8 by maintainers)
Top Results From Across the Web
How to configure model binding to get UTC DateTime via ...
When I tried to send DateTime object via form-data .NET-MVC binds it into DateTime object with the server's local timezone.
Read more >DateTimeOffset.UtcDateTime Property (System)
It converts the date and time of the current DateTimeOffset object to Coordinated Universal Time (UTC). The conversion is performed by subtracting the...
Read more >Converting between DateTime and DateTimeOffset
Based on its offset, it determines whether the DateTimeOffset value is a UTC time, a local time, or some other time and defines...
Read more >DateOnly in .NET 6 and ASP.NET Core 6
Use DateTime or DateTimeOffset and make sure TimeOfDay is Zero. And pay extra care when doing cross-timezone conversations.
Read more >Custom Model Binding in Asp.net Core, 2: Getting Time + ...
Once the form is posted a custom model binder combines both the DateTime and the client Time Zone Offset into an unique DateTimeOffset....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Referring to the statement here, I found that as long as the TryParseModelBinderProvider is remove first and then added, it will be normal
like this
Fyi @mkArtak