tags:

views:

212

answers:

3

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..

+2  A: 

GMT is, for all practical purposes, the same as UTC.

This means that you can just use one of the DateTime constructors that takes a DateTimeKind parameter, setting it to DateTimeKind.Utc.

DateTime gmt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
LukeH
UTC is not quite GMT - GMT obeys DLT (Daylight Savings), although it is equivalent much of the time.
Noldorin
UTC and GMT only differ by fractions of a second. GMT has no daylight savings time.
Erik
@Noldorin: GMT and UTC *are the same*, to all intents and purposes. GMT *does not* obey daylight savings: In the UK it is currently BST which is GMT+1. http://en.wikipedia.org/wiki/Greenwich_Mean_Time
LukeH
I think you should explain why this works.
Maxim Zaslavsky
+3  A: 

Check out the DateTimeOffset structure

EDIT: Actually, after further reading about DateTimeOffset, it might not be the most adequate solution for what you're trying to do... I think this is the most direct answer to your question :

DateTime epoch = DateTime.SpecifyKind(new DateTime(1970, 1, 1), DateTimeKind.Utc);
Thomas Levesque
Using DateTimeOffset would be a very interesting solution! Could you give us some sample code to show how you're intending to use it to find the GMT time? Are you going to find the offset between DateTime.Now and DateTime.UtcNow, and then apply the offset to January 1, 1970 in your own time zone? Or is there a more direct approach? Very interesting!
Maxim Zaslavsky
Also, could this approach (per my interpretation of your answer as explained in the above comment) work for every timezone that you want to convert to? In other words, is there something similar to DateTime.UtcNow available for other timezones? If not, i don't think this would be a viable universal solution.
Maxim Zaslavsky
@Thomas: I don't agree that this is "the most direct answer" to the question: You're constructing the `DateTime` and then calling the `SpecifyKind` method. The same result can be achieved just by calling a different constructor, with no need for the extra method call. (See my answer for details.)
LukeH
right, it's not really the "most direct"... however I find it more understandable than calling the constructor with 7 arguments
Thomas Levesque
+5  A: 

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.

Maxim Zaslavsky
GMT *does not* obey daylight savings time. http://en.wikipedia.org/wiki/Greenwich_Mean_Time
LukeH
Yup sorry, it was a mistake. Fixed.
Maxim Zaslavsky