I want to show Date & Time of an event which will be managed according to time zone of user. To check time zone I change my system time Zone to another time zone but my code is still getting Local time Zone. Here's My code
I am using Cassendra Database and C# .NET MVC
DateTime startTimeFormate = x.Startdate;
DateTime endTimeFormate = x.Enddate;
TimeZone zone = TimeZone.CurrentTimeZone;
DateTime startTime = zone.ToLocalTime(startTimeFormate);
DateTime endTime = zone.ToLocalTime(endTimeFormate);
To convert the UTC DateTime
to your Local DateTime
, you have to use TimeZoneInfo
as follows:
DateTime startTimeFormate = x.Startdate; // This is utc date time
TimeZoneInfo systemTimeZone = TimeZoneInfo.Local;
DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(startTimeFormate, systemTimeZone);
Moreover if you want to convert UTC DateTime
to user specific Local DateTime
then do as follows:
string userTimeZoneId = "New Zealand Standard Time";
TimeZoneInfo nzTimeZone = TimeZoneInfo.FindSystemTimeZoneById(userTimeZoneId);
DateTime userLocalDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, userTimeZoneId);
Note: TimeZone
in .NET
is obsolete now and it has been deprecated. Instead use TimeZoneInfo
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With