It's unclear what your trying to do. The CLR date/time support is, IMHO, poorly thought out. It would help if UTC dates and time were provided by default and would then have to get converted to local representations (for some definition of "local"). That would be almost ... Unix-like. But I digress.
Anyway, this might help:
To get the current instant in time, expressed relative to the current Time Zone:
DateTime localNow = DateTime.Now ;
To get the current instant in time, expressed as Zulu/GMT/UTC (Universel Temps Coordonné — not exactly idiomatic or grammatically correct French, but committees are good :^) at language butchery), you can say
DateTime utcNow = DateTime.UtcNow ;
(Note that a UTC time is constant and isn't dependent on timezone. If you had computers scattered around the world and configured for the local timezone
- Tokyo
- New York City
- Paris
- London
and they all executed the following line of code at exactly the same time:
DateTime now = DateTime.UtcNow ;
They would all have exactly the same value for now
.
If you've already got a local "now", you can say:
DateTime utcNow = localNow.ToUniversalTime() ;
Once you've got a "now" in terms of UTC, you can get the time in the time zone of choice by saying something like:
TimeZoneInfo desiredTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time") ;
DateTime desiredLocalNow = TimeZoneInfo.ConvertTime( utcNow , desiredTimeZone ) ;
You'll note that if you try to convert desiredLocalNow
to UTC, you'll get a different value for UTC time: this is because DateTime is **NOT** timezone-aware and its
Kindis set to
DatetimeKind.Local. To give things timezone awareness, you'll need to use
DateTimeOffset`, something along these lines:
DateTimeOffset utcNow = DateTimeOffset.UtcNow ;
DateTimeOffset localNow = utcNow.ToLocalTime() ;
TimeZoneInfo desiredTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time") ;
DateTimeOffset desiredLocalNow1 = TimeZoneInfo.ConvertTime( localNow , desiredTimeZone ) ;
DateTimeOffset desiredLocalNow2 = TimeZoneInfo.ConvertTime( utcNow , desiredTimeZone ) ;
Hope this helps!