Feb 27, 2023

C#: Convert any time to time in any timezone

You can use this to convert any server time to india time or any other time zone
here's possible list of all zone IDs which can be used in this method: https://nodatime.org/TimeZones

First add reference to NodaTime nuget package which is built by the ultimate Jon Skeet

Pun intended
1. Jon Skeet can divide by zero.
2. When Jon Skeet's code fails to compile the compiler apologises.
3. If Jon-Skeet's program fails to compile they fix the compiler.
4. Jon Skeet coded his last project entirely in Microsoft Paint, just for the challenge.
5. Jon Skeet keeps a daily backup of the Internet on his USB pendrive.

Jon Skeet handles all my exceptions
catch(Exception ex)

here's the method
public DateTime ConvertToTimeZoneFromUtc(DateTime utcDateTime, string timezone)
    var easternTimeZone = DateTimeZoneProviders.Tzdb[timezone];
    return Instant.FromDateTimeUtc(utcDateTime)

string zoneID = "Asia/Kolkata";
var date = ConvertToTimeZoneFromUtc(DateTime.Now.ToUniversalTime(), zoneID);

SkeetOverflow link: https://stackoverflow.com/questions/75580559/hot-to-change-the-default-datetime-date-of-app-hosted-on-cloud-server-side-to-m/75581290

No comments:

Post a Comment

Be the first to comment on this post.