The .NET Framework documentation is not clear whether or not the
DateTime construtors create a local time or a UTC time DateTime
instance. So I ran the following snippet:
DateTime t1 = new DateTime(1970, 1, 1, 0, 0, 0);
DateTime t2 = t1.ToUniversalTime();
Console.WriteLine("t1={0}, t2={1}", t1, t2);
and got this output (.NET 1.1):
t1=1/1/1970 12:00:00 AM, t2=1/1/1970 5:00:00 AM
This proved me that DateTime construtors create local time DateTime.
(This also proved that the documentation of TimeZone.GetUtcOffset
Method is wrong since the example states that new DateTime( 2000, 1, 1
) is a UTC time.)
So far, I'm using strings to create a UTC DateTime as follow:
DateTime t3 = DateTime.Parse("1970-01-01 00:00:00+0",
DateTimeFormatInfo.InvariantInfo);
DateTime t4 = t3.ToUniversalTime();
The ouput is:
t3=12/31/1969 7:00:00 PM, t4=1/1/1970 12:00:00 AM
Using strings is not a very elegant programming style. Does anybody
know how to create a UTC DateTime without a string???
The only thing I can possibly find is:
DateTime t5 = new DateTime(1970, 1, 1);
t5 = t5 - TimeZone.CurrentTimeZone.GetUtcOffset(t4);
which is not very elagant too.
Any hints will be greatly appreciated,
-- Laurent
Thomas Scheidegger [MVP] - 03 Dec 2003 01:28 GMT
Hi Laurent
> not clear whether or not the
> DateTime construtors create a local time or a UTC time DateTime
> instance.
check this:
Date and Time FAQ
Author Anthony Moore
Description Frequently Asked Questions about the DateTime, TimeSpan
and TimeZone classes in the System namespace.
http://www.gotdotnet.com/team/clr/bcl/techarticles/techarticles.aspx
Is a DateTime in Local or Universal time?
A common misconception about DateTime is that it represents a local time,
or can represent a universal time by switching to a distinct different mode.
In fact the DateTime is neither local nor universal.

Signature
Thomas Scheidegger - MVP .NET - 'NETMaster'
http://www.cetus-links.org/oo_dotnet.html - http://dnetmaster.net/