So this Publish time is a datetime in UTC that is retrieved from my database, it is currently in UTC and I need to change it to local time and then display the local time on my LineChartDataSet
CodePudding user response:
You can transform UTC dates to other timezone equivalents using a TimeZoneInfo
instance.
You can get the local timezone by using TimeZoneInfo.Local
.
Put together:
var yourTimeZone = TimeZoneInfo.Local;
var localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, yourTimeZone);
Pick another timezone to have your chart work for people in other regions, even on the same computer.
CodePudding user response:
You could use DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc).ToLocalTime()
Converts the value of the current DateTime object to local time.
https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tolocaltime?view=net-6.0
That is; it will translate the value to the local datetime of the server server. If all your users and the server are set to the same timezone, this works, else take a look at the other answer.
You need DateTime.SpecifyKind
because the date time coming from EF core has a Kind of DateTimeKind.Unspecified
, so we need to specify that the datetime is UTC before converting to local time.