Configuring TimeZoneInfo.ConvertTime to Accept an Explicit Source Time Zone for Unspecified DateTime Values
0 reputation · 25 Mar 2026, 09:55 UTC
0 reputation · 25 Mar 2026, 09:55 UTC
Goal: Convert a DateTime value whose Kind is Unspecified but which logically represents a specific time zone (e.g., Eastern Standard Time) to UTC while preserving the original zone’s identity for future conversions.
Constraint: TimeZoneInfo.ConvertTime(DateTime, TimeZoneInfo) interprets Unspecified as the system local zone and provides no overload to supply an explicit source time zone, forcing developers to either change the Kind property (which is unsafe) or perform manual offset calculations that lose the original time‑zone ID and cannot handle historic DST rule changes.
Questions: Is there a supported way to supply an explicit source time zone for an Unspecified DateTime without altering its Kind? Can the existing ConvertTime API be extended or wrapped to accept a source TimeZoneInfo while maintaining backward compatibility? What alternatives within .NET allow accurate zone‑aware conversion while retaining the original time‑zone identifier for later use?
29775 reputation · 25 Mar 2026, 21:00 UTC
Yes. Use the three‑parameter overload of TimeZoneInfo.ConvertTime that accepts a source and a destination TimeZoneInfo. It treats an Unspecified DateTime as if it belongs to the supplied source zone, converts it to the destination zone, and returns a DateTime with Kind=Unspecified. This preserves the original zone’s identity for future conversions.
The ConvertTime(DateTime, TimeZoneInfo, TimeZoneInfo) overload was added in .NET 3.5. It ignores the DateTime.Kind when a source zone is supplied, so you can pass an Unspecified value representing, say, Eastern Standard Time, and get the correct UTC value without touching the Kind property.
public static DateTime ConvertTime(DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone).DateTimeKind.Unspecified regardless of input kind.ArgumentException if the local time is ambiguous or invalid for the source zone unless you handle it with IsAmbiguousTime or IsInvalidTime first.var sourceZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");var utc = TimeZoneInfo.ConvertTime(myUnspecified, sourceZone, TimeZoneInfo.Utc);sourceZone.Id (or the TimeZoneInfo itself) alongside the DateTime for future round‑trips.TimeZoneInfo.ConvertTime(utc, TimeZoneInfo.Utc, sourceZone);.For projects that must support .NET 2.0, create an extension method that:
sourceZone.GetUtcOffset(dateTime) and adds/subtracts that offset, handling ambiguous/invalid times manually.Store the DateTimeOffset (which keeps the offset) together with the zone ID. This gives you the offset at that instant and the zone for future DST changes, but you still need the zone ID for correct historical conversions.
Which .NET framework version are you targeting? If you are constrained to .NET 2.0, the three‑parameter overload is unavailable and you must use the manual offset approach.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.