locked
Regarding UTC & Normal time c# RRS feed

  • Question

  • just see the below code

    Console.WriteLine (DateTime.UtcNow.ToLongTimeString());
    Console.WriteLine (DateTime.Now.ToLongTimeString());
    Console.WriteLine (DateTimeOffset.Now);

    output:

    09:07:35
    04:07:35
    2014-12-08 04:07:35 -05:00

    this line DateTime.UtcNow.ToLongTimeString() gives 09:07:35 which is not my pc time

    this line DateTime.Now.ToLongTimeString() gives 04:07:35 this is my pc time.

    i just like to know what causes the difference in time like 7 hrs between two time.

    this line DateTimeOffset.Now showing my pc time 04:07:35 but show a extra data like -05:00 what does it means ?

    suppose if i have utc time and this one -05:00 then how can i calculate user pc time means my pc time 04:07:35. please discuss this calculation with sample code.

    Tuesday, December 9, 2014 8:04 AM

Answers

  • UTC time formerly GMT is the time based on a 24 hours clock where 12 is the sun in top in Greenwich London. That is the time notation which is in AFAIK in summer nowhere used in the world and in the winter in Great Britain, Ireland and Portugal. 

    All other times are an offset of that. 

    You live about 5 hours (in the rotation speed of the world) on a longitude east of London. (Not India but somewhere north or north west of that)


    Success
    Cor


    • Edited by Cor Ligthert Tuesday, December 9, 2014 10:01 AM
    • Proposed as answer by Magnus (MM8)MVP Tuesday, December 9, 2014 3:02 PM
    • Marked as answer by Barry Wang Tuesday, December 16, 2014 10:21 AM
    Tuesday, December 9, 2014 9:54 AM
  • Just to expand on Cor's answer, UTC stands for Coordinated Universal Time (in French). It is a standard 'clock' that can be used anywhere in the world to compare two times and see which one occurred first (or if they occurred together) regardless of time zone.

    There are methods in C# that convert from local to UTC time and back again (e.g. http://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime%28v=vs.110%29.aspx). In any system you will generally always want to store times in UTC. That way if someone else looks up the time from another timezone, it is easy to convert back to local time again and know when something happened. I.e. If I create a record at 10.53 am in the UK (which is currently identical to UTC), someone in the Eastern USA (which is 5 hours behind UTC) will want to look at that record and know that it was created at 5.53 am in their local time.


    • Edited by RJP1973 Tuesday, December 9, 2014 11:03 AM
    • Proposed as answer by Magnus (MM8)MVP Tuesday, December 9, 2014 3:02 PM
    • Marked as answer by Barry Wang Tuesday, December 16, 2014 10:21 AM
    Tuesday, December 9, 2014 11:00 AM

All replies

  • UTC time formerly GMT is the time based on a 24 hours clock where 12 is the sun in top in Greenwich London. That is the time notation which is in AFAIK in summer nowhere used in the world and in the winter in Great Britain, Ireland and Portugal. 

    All other times are an offset of that. 

    You live about 5 hours (in the rotation speed of the world) on a longitude east of London. (Not India but somewhere north or north west of that)


    Success
    Cor


    • Edited by Cor Ligthert Tuesday, December 9, 2014 10:01 AM
    • Proposed as answer by Magnus (MM8)MVP Tuesday, December 9, 2014 3:02 PM
    • Marked as answer by Barry Wang Tuesday, December 16, 2014 10:21 AM
    Tuesday, December 9, 2014 9:54 AM
  • Just to expand on Cor's answer, UTC stands for Coordinated Universal Time (in French). It is a standard 'clock' that can be used anywhere in the world to compare two times and see which one occurred first (or if they occurred together) regardless of time zone.

    There are methods in C# that convert from local to UTC time and back again (e.g. http://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime%28v=vs.110%29.aspx). In any system you will generally always want to store times in UTC. That way if someone else looks up the time from another timezone, it is easy to convert back to local time again and know when something happened. I.e. If I create a record at 10.53 am in the UK (which is currently identical to UTC), someone in the Eastern USA (which is 5 hours behind UTC) will want to look at that record and know that it was created at 5.53 am in their local time.


    • Edited by RJP1973 Tuesday, December 9, 2014 11:03 AM
    • Proposed as answer by Magnus (MM8)MVP Tuesday, December 9, 2014 3:02 PM
    • Marked as answer by Barry Wang Tuesday, December 16, 2014 10:21 AM
    Tuesday, December 9, 2014 11:00 AM