locked
looking for clarification for DateTimeOffset usage RRS feed

  • Question

  • User-1223304583 posted

    see my code

            private void button1_Click(object sender, EventArgs e)
            {
                string strDateTime = "25/01/2017 06:25:00 AM";
                DateTime localDateTime = DateTime.Parse(strDateTime);
                DateTime univDateTime = localDateTime.ToUniversalTime();
                var utc = DateTimeOffset.Parse(univDateTime.ToString());
    
            }

    see screen shot

    i know that date time offset is difference between UTC time and local time.

    when i convert 25/01/2017 06:25:00 AM to utc then i got 25-01-2017 12:55:00. so 06:25:00 becomes 12:55:00.

    when i try to get offset then it is being +05:30 and this is not clear.

    so what is going on the above code anyone can explain please.

    thanks

    Wednesday, January 25, 2017 12:44 PM

Answers

  • User1771544211 posted

    Hi mamoni,

    so what is going on the above code anyone can explain please.

    string strDateTime = "25/01/2017 06:25:00 AM";

    DateTime localDateTime = DateTime.Parse(strDateTime); //In this line you convert the string to DateTime

    DateTime univDateTime = localDateTime.ToUniversalTime(); //Converts the value of the current DateTime object to Coordinated Universal Time (UTC).

    The following link talks about how does DateTime.ToUniversalTime() work,please take it as reference.

    http://stackoverflow.com/questions/1201378/how-does-datetime-touniversaltime-work

    var utc = DateTimeOffset.Parse(univDateTime.ToString()); //Converts the specified string representation of a date, time, and offset to its DateTimeOffset equivalent.

    when i try to get offset then it is being +05:30 and this is not clear.

    You can learn more about the offset in the following link.

    https://en.wikipedia.org/wiki/UTC_offset

    Best Regards,

    Jean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 26, 2017 2:31 AM
  • User475983607 posted

    please run my code at your end and tell me where i made the mistake. tell me what i need to fix in my code. thanks

    It seems you're having difficulty with math.  

    12:55:00 - 05:30 = 06:25:00 

    or 

    06:25 + 05:30 = 12:55:00

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 1, 2017 6:57 PM

All replies

  • User1771544211 posted

    Hi mamoni,

    so what is going on the above code anyone can explain please.

    string strDateTime = "25/01/2017 06:25:00 AM";

    DateTime localDateTime = DateTime.Parse(strDateTime); //In this line you convert the string to DateTime

    DateTime univDateTime = localDateTime.ToUniversalTime(); //Converts the value of the current DateTime object to Coordinated Universal Time (UTC).

    The following link talks about how does DateTime.ToUniversalTime() work,please take it as reference.

    http://stackoverflow.com/questions/1201378/how-does-datetime-touniversaltime-work

    var utc = DateTimeOffset.Parse(univDateTime.ToString()); //Converts the specified string representation of a date, time, and offset to its DateTimeOffset equivalent.

    when i try to get offset then it is being +05:30 and this is not clear.

    You can learn more about the offset in the following link.

    https://en.wikipedia.org/wiki/UTC_offset

    Best Regards,

    Jean

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

    please run my code at your end and tell me where i made the mistake. tell me what i need to fix in my code. thanks

    Monday, January 30, 2017 11:05 AM
  • User475983607 posted

    please run my code at your end and tell me where i made the mistake. tell me what i need to fix in my code. thanks

    It seems you're having difficulty with math.  

    12:55:00 - 05:30 = 06:25:00 

    or 

    06:25 + 05:30 = 12:55:00

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 1, 2017 6:57 PM