[Solved] Convert LocalTime to UTC showing wrong time

on Jun 29, 2022 04:09 AM


I am converting the DateTime from local time to utc and time zone am passing is est

The date time which i am passing is (06/29/2022 01:00:00 AM) after conversion its showing (06/29/2022 05:00:00 AM), but for 1:00:00 AM local time to est is 3:30 PM but its showing 5:00 AM.

GMT_Standard_Time = "GMT Standard Time"

dtLocaltime = 6/29/2022 1:00:00 AM

strTimezone = Eastern Standard Time 

Its showing this -> 6/29/2022 5:00:00 AM

What conversion is happening here?

Why it’s showing this wrong time?

Could you please explain me this?

public static DateTime ConvertLocalTimeToUTC(DateTime dtLocalTime, string strTimezone)
    TimeZoneInfo timeZoneInfo;
    DateTime dtUTCTime = new DateTime();
    DateTime dtTimezoneTime;
    if (strTimezone != "")
            //select * from FEV_TIMEZONE where timezone='CST' and timezone_id in(68,69);
            //FindSystemTimeZoneById in the list Mexico is optional so will not get the result
            if (strTimezone == "Mexico Standard Time")
                strTimezone = "Central Standard Time";
            //select * from FEV_TIMEZONE where utc_offset='-07:00:00' and timezone_idin(72,73);
            if (strTimezone == "Mexico Standard Time 2")
                strTimezone = "Mountain Standard Time";
            if (GMT_Standard_Time == strTimezone)
                dtUTCTime = dtLocalTime;
                timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(strTimezone);
                dtUTCTime = TimeZoneInfo.ConvertTimeToUtc(dtLocalTime, timeZoneInfo);
        catch (Exception ex)
            timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(strTimezone);
            dtTimezoneTime = TimeZoneInfo.ConvertTime(dtLocalTime, timeZoneInfo);
            dtUTCTime = TimeZoneInfo.ConvertTimeToUtc(dtTimezoneTime, timeZoneInfo);
    return dtUTCTime;

Above is my code.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Jun 30, 2022 02:03 AM
zeeshanpas says:
dtUTCTime = TimeZoneInfo.ConvertTimeToUtc(dtTimezoneTime, timeZoneInfo);

Use ConvertTime instead of ConvertTimeToUtc method.

ConvertTime converts specified time to the time in a particular time zone.

ConvertTimeToUtc  converts the specified date and time to Coordinated Universal Time (UTC).

Replace above with below code.

dtUTCTime = TimeZoneInfo.ConvertTime(dtTimezoneTime, timeZoneInfo);

For more details on ConvertTimetoUtc refer ConvertTimeToUtc.

For more details on ConvertTime refer ConvertTime.