locked
Get date in my current timezone RRS feed

  • Question

  • User2108892867 posted

    Hello everyone, I have a question about this code:

     TimeZoneInfo nzZone = TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time");
                                mySetting.my_date = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, nzZone);
                                

    I just could not understand because when it's on local machine, it returns 21/05/2018 which is correct. But accessing the page on the hosted site, it returns 22/05/2018. I thought because I used Utc time, it should be able to convert to my current time zone and disregard the server time. Did I miss anything?

    Thanks

    Monday, May 21, 2018 1:19 AM

Answers

  • User36583972 posted


    Hi asplearning,

    asplearning

    js.Serialize(TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, nzZone))

    This bit should at lease produced similar result cause they are all converted from UtcNow. But they are not similar at all. One is Date(1526868924867) and the other one is Date(1526930161581). 1526868924867 - 1526930161581 is like 17 hours different. My question is why is that the case?


    I have made a test on my side. I have Deployed my app to the Azure server, located on the West US.

    When I use the following code to Serialize the DateTime.UtcNow, I got the same problem as you.

    js.Serialize(TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, nzZone))

    I found the javascriptserializer datetime will return the total millisecond from January 1, 1970 to the actual date. Why host site and local are different, It's so strange. But, you can avoid this issue, try the following code.

             TimeZoneInfo nzZone = TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time");
                Label1.Text = "New Zealand Standard Time: " + TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, nzZone).ToString("yyyy-MM-dd HH:mm:ss");
                JavaScriptSerializer js = new JavaScriptSerializer();
                string jsstringdata = js.Serialize(TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, nzZone));
                Label2.Text = "JavaScriptSerializer New Zealand Standard Time: " + jsstringdata;
    
                jsstringdata = Regex.Replace(jsstringdata, @"\\/Date\((\d+)\)\\/", match =>
                {
                    DateTime dt = new DateTime(1970, 1, 1);
                    dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
                    dt = dt.ToLocalTime();
                    return dt.ToString("yyyy-MM-dd HH:mm:ss");
                });
    
                Label3.Text = "JavaScriptSerializer New Zealand Standard Time Regex format: " + jsstringdata;

    Best Regards,

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 28, 2018 8:21 AM

All replies

  • User2108892867 posted

    I tried to debug this by putting literal control and the control will display the UTC time and json date after serialized.

    JavaScriptSerializer js = new JavaScriptSerializer();
                TimeZoneInfo nzZone = TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time");
    
                litUTC.Text = DateTime.UtcNow.ToString() + " " + js.Serialize(DateTime.UtcNow) + " " + js.Serialize(TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, nzZone));

    I could not figure out why. On my local machine, it displayed:

    21/05/2018 2:15:24 AM "\/Date(1526868924867)\/" "\/Date(1526868924867)\/"

    And on the hosted site, it displayed:

    21/05/2018 2:16:01 a.m. "\/Date(1526868961581)\/" "\/Date(1526930161581)\/"

    Any ideas why this happens? Date(1526930161581) should be the same or similar to Date(1526868961581). Both of them are converted from UTCNow to my timezone. 

    Thanks

    Monday, May 21, 2018 2:17 AM
  • User36583972 posted


    Hi asplearning,

    And on the hosted site, it displayed:

    21/05/2018 2:16:01 a.m. "\/Date(1526868961581)\/" "\/Date(1526930161581)\/"

    Where your hosted site have to be deployed? This time will not be the same unless tested at the same time point and with the same time zone.

    UTC: Universal Time Coordinated / Universal Coordinated Time. 0° (Prime Meridian). It is the primary time standard by which the world regulates clocks and time.

    The code you used is no error.

    DateTime.ToUniversalTime Method ():
    https://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime(v=vs.110).aspx

    Is there anything else that leads to this problem? Please include a detailed description about the results including any exception messages.


    Best Regards,

    Yong Lu

    Tuesday, May 22, 2018 4:14 AM
  • User2108892867 posted

    Thanks for the reply. This is what I understand about UTC and please correct me if I am wrong. It doesn't matter where I am, UTC is a standard time. If it's 10:30, it's 10:30 every where in the world. So what I am trying to do to get my current time is to get UTC time and convert that time to my time zone and this how I can get the current time:

    JavaScriptSerializer js = new JavaScriptSerializer();
    TimeZoneInfo nzZone = TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time");
    litUTC.Text = DateTime.UtcNow.ToString() + " " + js.Serialize(DateTime.UtcNow) + " " + js.Serialize(TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, nzZone));

    If you look at this part

    js.Serialize(DateTime.UtcNow)

    Both my local and hosted one produced almost the same result: Date(1526868924867) and Date(1526868961581). So what that means is this one:

    js.Serialize(TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, nzZone))

    This bit should at lease produced similar result cause they are all converted from UtcNow. But they are not similar at all. One is Date(1526868924867) and the other one is Date(1526930161581). 1526868924867 - 1526930161581 is like 17 hours different. My question is why is that the case?

    Thanks

    Tuesday, May 22, 2018 9:35 PM
  • User36583972 posted


    Hi asplearning,

    asplearning

    js.Serialize(TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, nzZone))

    This bit should at lease produced similar result cause they are all converted from UtcNow. But they are not similar at all. One is Date(1526868924867) and the other one is Date(1526930161581). 1526868924867 - 1526930161581 is like 17 hours different. My question is why is that the case?


    I have made a test on my side. I have Deployed my app to the Azure server, located on the West US.

    When I use the following code to Serialize the DateTime.UtcNow, I got the same problem as you.

    js.Serialize(TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, nzZone))

    I found the javascriptserializer datetime will return the total millisecond from January 1, 1970 to the actual date. Why host site and local are different, It's so strange. But, you can avoid this issue, try the following code.

             TimeZoneInfo nzZone = TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time");
                Label1.Text = "New Zealand Standard Time: " + TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, nzZone).ToString("yyyy-MM-dd HH:mm:ss");
                JavaScriptSerializer js = new JavaScriptSerializer();
                string jsstringdata = js.Serialize(TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, nzZone));
                Label2.Text = "JavaScriptSerializer New Zealand Standard Time: " + jsstringdata;
    
                jsstringdata = Regex.Replace(jsstringdata, @"\\/Date\((\d+)\)\\/", match =>
                {
                    DateTime dt = new DateTime(1970, 1, 1);
                    dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
                    dt = dt.ToLocalTime();
                    return dt.ToString("yyyy-MM-dd HH:mm:ss");
                });
    
                Label3.Text = "JavaScriptSerializer New Zealand Standard Time Regex format: " + jsstringdata;

    Best Regards,

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 28, 2018 8:21 AM
  • User2108892867 posted

    Thank you Yohann Lu. That's very helpful. I did some research and it seems this is the problem with js serializer. A lot of people said it's buggy. So now I have decided to go with JsonConvert.SerializeObject. 

    Thanks

    Wednesday, May 30, 2018 9:19 PM