Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / December 2003

Tip: Looking for answers? Try searching our database.

Creating UTC DateTime

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Laurent - 01 Dec 2003 17:40 GMT
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/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.