locked
C# How to convert UTC date time to Mexico date time RRS feed

  • Question

  • User-1223304583 posted

    see my code which i used to convert Mexico date and time to UTC date and time.

    string strDateTime = "25/01/2017 07:31:00 AM";
        DateTime localDateTime = DateTime.Parse(strDateTime);
        DateTime univDateTime = localDateTime.ToUniversalTime();
    ToUniversalTime return UTC 25-01-2017 02:01:00 when again i try to convert the same UTC date and time UTC 25-01-2017 02:01:00 
    to Mexico local time then i got 24-01-2017 06:01:00

    so see

    07:31:00 AM becomes 06:01:00

    which is not right. so tell me what is missing in my code for which i am getting wrong local time when i convert from utc to Mexico time using timezone info.

    see my code which converting from utc to Mexico local time using timezone info.

        string strDateTime = "25-01-2017 02:01:00";
        DateTime utcDateTime = DateTime.Parse(strDateTime);
        string nzTimeZoneKey = "Pacific Standard Time (Mexico)";
        TimeZoneInfo nzTimeZone = TimeZoneInfo.FindSystemTimeZoneById(nzTimeZoneKey);
        DateTime nzDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, nzTimeZone);

    Wednesday, January 25, 2017 2:10 PM

Answers

  • User-1838255255 posted

    Hi mamoni.kol2017,

    According to your description and code, I make a sample, please refer to this sample:

    1. you need get this this region time zone.

    2. get the UTC time, then minus this time zone, get Mexico("Central Standard Time") time.  

    Sample Code:

    protected void Button1_Click(object sender, EventArgs e)
            {
                TimeZoneInfo timeZoneInfo;
    
                DateTime dateTime;
    
                //Set the time zone information to US Mountain Standard Time 
    
                timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    
                //Get date and time in US Mountain Standard Time 
    
                dateTime = TimeZoneInfo.ConvertTime(DateTime.Now.ToUniversalTime(), timeZoneInfo);
    
                //Print out the date and time
    
                Response.Write(dateTime.ToString("yyyy-MM-dd HH-mm-ss"));
    
            }

    About time zone, please refer to this link:

    http://www.xiirus.net/articles/article-_net-convert-datetime-from-one-timezone-to-another-7e44y.aspx 

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 26, 2017 3:02 AM
  • User-1838255255 posted

    Hi mamoni.kol2017,

    According to your description and code, you could convert Mexico time to UTC time in ConvertTimeToUtc method.

    Here is the sample code:

    protected void Button1_Click(object sender, EventArgs e)
            {
                string strDateTime = "2017-01-25 02:01:00";
                DateTime utcDateTime = DateTime.Parse(strDateTime);
                string nzTimeZoneKey = "Pacific Standard Time (Mexico)";
                TimeZoneInfo nzTimeZone = TimeZoneInfo.FindSystemTimeZoneById(nzTimeZoneKey);
                DateTime nzDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, nzTimeZone);
    DateTime NewutcDateTime = TimeZoneInfo.ConvertTimeToUtc(nzDateTime,nzTimeZone); }

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 7, 2017 1:01 PM

All replies

  • User-1838255255 posted

    Hi mamoni.kol2017,

    According to your description and code, I make a sample, please refer to this sample:

    1. you need get this this region time zone.

    2. get the UTC time, then minus this time zone, get Mexico("Central Standard Time") time.  

    Sample Code:

    protected void Button1_Click(object sender, EventArgs e)
            {
                TimeZoneInfo timeZoneInfo;
    
                DateTime dateTime;
    
                //Set the time zone information to US Mountain Standard Time 
    
                timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    
                //Get date and time in US Mountain Standard Time 
    
                dateTime = TimeZoneInfo.ConvertTime(DateTime.Now.ToUniversalTime(), timeZoneInfo);
    
                //Print out the date and time
    
                Response.Write(dateTime.ToString("yyyy-MM-dd HH-mm-ss"));
    
            }

    About time zone, please refer to this link:

    http://www.xiirus.net/articles/article-_net-convert-datetime-from-one-timezone-to-another-7e44y.aspx 

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 26, 2017 3:02 AM
  • User-1223304583 posted

    from your code it seems you are converting server side date and time to Mexico date and time.

    my objective was different. first i convert a Mexico date and time to UTC date and time and later i try to convert that UTC date and time to Mexico local date and time but i was getting different output. my code was posted above. please try with that one and tell me what was wrong there. thanks

    Monday, January 30, 2017 11:03 AM
  • User-1838255255 posted

    Hi mamoni.kol2017,

    According to your description and code, you could convert Mexico time to UTC time in ConvertTimeToUtc method.

    Here is the sample code:

    protected void Button1_Click(object sender, EventArgs e)
            {
                string strDateTime = "2017-01-25 02:01:00";
                DateTime utcDateTime = DateTime.Parse(strDateTime);
                string nzTimeZoneKey = "Pacific Standard Time (Mexico)";
                TimeZoneInfo nzTimeZone = TimeZoneInfo.FindSystemTimeZoneById(nzTimeZoneKey);
                DateTime nzDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, nzTimeZone);
    DateTime NewutcDateTime = TimeZoneInfo.ConvertTimeToUtc(nzDateTime,nzTimeZone); }

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 7, 2017 1:01 PM