Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Greenwich Mean Time

Tags:

c#

datetime

Is there any way to get the date January 1, 1970 Greenwich Mean Time in a datetime?

if i just specify new date(1970, 1, 1) i will get it with my current time zone..

like image 775
Peter Avatar asked Sep 12 '09 22:09

Peter


People also ask

Is GMT now UTC?

Prior to 1972, this time was called Greenwich Mean Time (GMT) but is now referred to as Coordinated Universal Time or Universal Time Coordinated (UTC). It is a coordinated time scale, maintained by the Bureau International des Poids et Mesures (BIPM). It is also known as "Z time" or "Zulu Time".

What is the GMT format?

GMT is a time zone officially used in some European and African countries. The time can be displayed using both the 24-hour format (0 - 24) or the 12-hour format (1 - 12 am/pm). UTC is not a time zone, but a time standard that is the basis for civil time and time zones worldwide.

Why is GMT 0?

The recommendation was based on the argument that naming Greenwich as Longitude 0º would be of advantage to the largest number of people. As the reference for GMT, the Prime Meridian at Greenwich therefore became the centre of world time and the basis for the global system of time zones.

How do I convert GMT to local time?

(GMT-5:00) Eastern Time (US & Canada)Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.). Note The date also follows UTC format.


1 Answers

GMT is equal to UTC (Coordinated Universal Time), more or less (unless you're dealing with fractions of seconds, there's no difference). DateTimeKind, an enumeration which lets you choose whether to display time in your local time zone or in UTC, is built into the DateTime constructor. Using this, we can achieve the equivalent of GMT.

The constructor we're looking to use is the following:

DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind)

or (in one there is a millisecond argument, in the other there isn't):

DateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind)

To obtain a DateTime for January 1, 1970 in UTC, this is what we can use:

DateTime inGMT = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); //using 1st constructor above

or:

DateTime inGMT = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); //using 2nd constructor above

NOTE: The contents of the enumeration DateTimeKind are below:

  • Unspecified (can be either local time or UTC)
  • Utc
  • Local

UPDATE: Thomas Levesque proposed a very creative solution in his answer, however I'm not sure if it is the most direct method and if it's a viable method that can be used for any time zone. What I think he's saying is that you can calculate the DateTimeOffset from DateTime.Now and DateTime.UtcNow, and apply the calculated offset to January 1, 1970 in your own time zone, which gives you it in UTC/GMT time. I'm not sure that there are simple methods to calculating offsets to other time zones, and then this becomes a bit redundant to the question.

UPDATE #2: I added another DateTime constructor which accomplishes the same thing, but lacks the millisecond argument. They are interchangable.

like image 126
Maxim Zaslavsky Avatar answered Sep 20 '22 09:09

Maxim Zaslavsky