0

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);
Mou
  • 15,673
  • 43
  • 156
  • 275
  • Can you check what result you get when you type `TimeZoneInfo.Local`? – Ghasan غسان Jan 25 '17 at 14:13
  • As an aside - note that `"Pacific Standard Time (Mexico)"` is for the Northern part of the Baja California peninsula. Cities include Tijuana and Mexicali, and it's called "Zona Noroeste" in Spanish. It is *not* Mexico's "Zona Pacifico". See [Wikipedia's Time in Mexico article](https://en.wikipedia.org/wiki/Time_in_Mexico). Mexico has four time zones, so be sure to pick the right one. – Matt Johnson-Pint Jan 25 '17 at 22:36
  • Also not there is no logical difference between the Windows time zone IDs `"Pacific Standard Time (Mexico)"` and `"Pacific Standard Time"`. It used to be thought that some part of the Mexico one used Mexico's DST transition dates (instead of the US transition dates), but that turned out not to be true and the data was corrected. That whole area is aligned with the US DST transitions (not the Mexico ones). – Matt Johnson-Pint Jan 25 '17 at 22:38
  • so @Matt guide what code i should use to convert UTC date time to Mexico local date time wherever my application is running. looking for your further guidance. – Mou Jan 27 '17 at 08:58
  • @MattJohnson please visit this link https://www.google.co.in/?gws_rd=ssl#q=mexico+date+and+time and tell me what time zone google use to show the Mexico date and time ? – Mou Jan 27 '17 at 09:01
  • 1
    @Mou - look carefully at the results of that link. Notice it has chosen Mexico City as the main answer, and given you alternate answers also. You have to know *where* in Mexico you want the time for. The entire country is not on the same time! For the capital, Mexico City, you would use `"Central Standard Time (Mexico)"`. There are actually 7 different Windows zones that might apply in Mexico, as some Mexican locations near the US border use US DST rules instead of Mexican ones. If you want a comprehensive list, ask a new question specifically about this. – Matt Johnson-Pint Jan 27 '17 at 18:30
  • @MattJohnson can't we convert any utc date and time to mexico local time providing timezone info ? long time back i work in a project where i use noda time library and i convert any country local time to other country local date time just passing timezone name. i grab the timezone name using js library called jstz.js and i got a success but this time i am trying to convert any UTC date time to any country local date time just by using .net bcl with time zone related classes. please tell me what you think. – Mou Jan 30 '17 at 09:19
  • You seem to be under the false assumption that there is only one time zone per country. Yes, you can convert UTC to one of the Mexican time zones, but you have to know which one! – Matt Johnson-Pint Jan 30 '17 at 18:18

3 Answers3

2

Okay, I didn't know that you were located in India - which changes things a little bit:

You're going to want to utilize the TimeZoneInfo.ConvertTime() API for this one.. Maybe something like :

    var dt = new DateTime(2017, 01, 25, 7, 31, 0).ToUniversalTime();
    var nzTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time (Mexico)");
    //var ist = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
    DateTime nzDateTime = TimeZoneInfo.ConvertTime(dt, TimeZoneInfo.Utc, nzTimeZone);
Mark C.
  • 6,332
  • 4
  • 35
  • 71
  • if my date time format is wrong then how datetime parse can parse it ? – Mou Jan 25 '17 at 14:01
  • Well if your code ran successfully, it's parsing it alright, but not in the manner that you're expecting. Please see the date formats from [MSDN](https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx) - you won't even see the string format of the date you're trying to use – Mark C. Jan 25 '17 at 14:05
  • i execute your code but still i am getting wrong time that is `06:01:00` instead of `07:31:00`. if u like i can upload the screen shot of my debugging screen of VS2013 IDE – Mou Jan 25 '17 at 14:08
  • What value are you getting for `univDateTime` ? And what are your local settings? – Mark C. Jan 25 '17 at 14:09
  • 25-01-2017 02:01:00 is stored in univDateTime variable. my pc timezone setting is UTC+05:30 – Mou Jan 25 '17 at 14:13
  • Try my updated answer - if it doesn't work let me know. – Mark C. Jan 25 '17 at 14:45
2

You current time zone (UTC+05:30) is different from the time zone you are converting into (UTC-8:00). So you get the difference. There is about 13 hours and 30 minutes difference from your original time zone to the targeted one. 5:30 - (-8) = 13:30.

Subtract 13 hours and 30 minutes from your original date, and then you get 18:01:00, which in 12-hour format is 6PM on the previous day.

Edit:

Instead of hard-coding Mexico time zone, you will need to have a method by which you can determine user's time zone no matter where they are coming from. This is best done using JavaScript as outlined in this answer.

Ghasan غسان
  • 5,577
  • 4
  • 33
  • 44
  • how to do it programmatically. would you please post a code to achieve it. thanks – Mou Jan 25 '17 at 14:27
  • i do not want to hard code `Subtract 13 hours and 30 minutes` i want to convert UTC date and time to client local time providing timezone info. – Mou Jan 25 '17 at 14:28
  • 1
    There is no hard coding here. That is the correct client local time corresponding to your local time, when you are at morning, they are at night, there is no wrong in this. So the code is fine. The real question is, what are you trying to achieve? – Ghasan غسان Jan 25 '17 at 14:31
  • 1
    for my web application user may arrive from any timezone. so i need to show utc date & time stored in variable to client local date and time. how to achieve it without hard code value that i need to add or Subtract from my original date and time. please guide me with sample code. – Mou Jan 25 '17 at 14:34
  • 1
    If you want to show the local date & time of the user, then what you did is correct; you detect where they are coming from, and pass that to `TimeZoneInfo`, and get the local date. So instead of hard coding Mexico time zone, you have to have a generic method to return the current time zone. I have updated my answer to include some description. – Ghasan غسان Jan 25 '17 at 14:43
  • @Mou - the key here is what you finally just mentioned: `"for my web application user may arrive from any timezone"`. That changes everything. You called `ToUniversalTime` with the invalid assumption that the server could somehow tell what your client's time zone is. It cannot. It is simply converting from the local *server* time zone to UTC. – Matt Johnson-Pint Jan 25 '17 at 22:29
1

Your problem is that the Parse is done without specifying the timezone it comes from - therefore the system will use whatever the default is of your computer. It appears that Your computer is NOT in PST. Rather somewhere in India.

Therefore after turning it into a DateTime object you need to convert it to UTC by specifying the PST timezone. There are a few ways to do this:

  1. Specify the timezone offset as part of the string.
  2. Call one of the TimeZoneInfo.ConvertTimeToUtc and specify the timezoneid

Maybe all you want to do is convert between two timezones by calling ConvertTime or ConvertTimeByTimeZoneId.


    string pst = "Pacific Standard Time";
    TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, pst));

For example: 7:30AM PST should be 1:30 UTC - not 2:30. So that suggests a problem in the initial conversion. 2 AM UTC to PST is indeed 6 PM. Also I noticed your input was 7:31 and you claim it output 2:01 -- does Mexico do 30 minute timezones? I know India does.

I use Google to test conversions by literally searching for "2:01 UTC to PST" and it returns the answer for comparison.

See this other post which shows declaring the input timezone for Parsing. And as stated one does NOT need to convert for DST. Does ConvertTimeFromUtc() and ToUniversalTime() handle DST?

More info on MSDN for TimeZoneInfo: https://msdn.microsoft.com/en-us/library/bb495915(v=vs.110).aspx

Community
  • 1
  • 1
ripvlan
  • 460
  • 6
  • 14
  • what you said here not very clear to me to rectify my code example. it will be helpful if you see my posted code and post here after rectifying it. basically i want to convert utc date and time to Mexico date and time. – Mou Jan 27 '17 at 09:05