Webapi: .net core 2 - How to manage time zones?

Created on 13 Feb 2018  路  3Comments  路  Source: OData/WebApi

I am using OData V7.0.0 for ASP.NET Core 2.x.

1) How can I tell the API to return and format dates as UTC when using the DateTime type? Currently, all dates are automatically formatted using the local time zone which is not the expected behavior.

2) Even when using DateTimeOffset on open properties, the time zone is ignored and the dates are still formatted using the local time zone.

BTW, following the documentation, it is impossible to use the SetTimeZoneInfo method since there is no HttpConfiguration in .net core 2.

Any help would be appreciated. Thank you!

question

Most helpful comment

Use this code
c# app.UseMvc(b=>{ b.SetTimeZoneInfo(TimeZoneInfo.Utc); b.MapODataServiceRoute(....); })

All 3 comments

Use this code
c# app.UseMvc(b=>{ b.SetTimeZoneInfo(TimeZoneInfo.Utc); b.MapODataServiceRoute(....); })

@genusP - Thank you for your help, it works perfectly! I am also able to use DateTimeOffset in case I need to use a specific time zone.

@genusP Can change that to only return date?

Was this page helpful?
0 / 5 - 0 ratings