How to convert UTC/GMT time to local time in c#

How to convert UTC/GMT time to local time in c#

 

Once the system knows its in UTC time, you can just call ToLocalTime 

DateTime dt = convertedDate.ToLocalTime();

 

DateTime convertedDate = DateTime.SpecifyKind(
    DateTime.Parse(dateStr),
    DateTimeKind.Utc);

var dtKind = convertedDate.Kind;

 

TimeZone.CurrentTimeZone.ToLocalTime(date);

 

Web service or some other source with a known format, you might want to consider something like this

DateTime.ParseExact(dateString, 
                   "MM/dd/yyyy HH:mm:ss", 
                   CultureInfo.InvariantCulture, 
                   DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal)

 

DateTime converted_Date = DateTime.Parse(date);
DateTime localDate = converted_Date.ToLocalTime();

 

DaylightTime day_light = TimeZone.CurrentTimeZone.GetDaylightChanges(year);

 

DateTime KnowThisIsUtc = whatever;
DateTime runtimeKnowsThisIsUtc = DateTime.SpecifyKind(KnowThisIsUtc, DateTimeKind.Utc);
DateTime localVersion = runtimeKnowsThisIsUtc.ToLocalTime();

 

Try this

 

public static DateTime UTCToPacific(DateTime dateTime)
        {
            return TimeZoneInfo.ConvertTimeFromUtc(dateTime, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));
        }

2021-10-03