using System.Runtime.InteropServices;
public static class Program
public static void Main()
public static void ExampleMethodUsage()
TimeZoneInfo eastTz = GetEasternTimeZoneInfo();
TimeZoneInfo centralTz = GetCentralTimeZoneInfo();
TimeZoneInfo pacificTz = GetPacificTimeZoneInfo();
TimeZoneInfo serverTz = GetServerTimeZoneInfo();
TimeZoneInfo sqlTz = eastTz;
DateTime clientDt = DateTime.Parse("2025-01-01T13:45:58-07:00");
DateTime serverDt = DateTime.Now;
DateTime sqlDt = DateTime.Parse("01/01/2025 13:45:58");
DateOnly sqlDo = DateOnly.Parse("01/01/2025");
TimeOnly sqlTo = TimeOnly.Parse("13:45:58");
string sClientDt = clientDt.ToEasternTimeZoneString(serverTz);
string sServerDt = serverDt.ToEasternTimeZoneString(serverTz);
string sSqlDt = sqlDt.ToEasternTimeZoneString(sqlTz);
string sSqlDo = sqlDo.ToEasternTimeZoneString();
string sSqlTo = sqlTo.ToEasternTimeZoneString(sqlTz);
Console.WriteLine(sClientDt);
Console.WriteLine(sServerDt);
Console.WriteLine(sSqlDt);
Console.WriteLine(sSqlDo);
Console.WriteLine(sSqlTo);
public static string ToEasternTimeZoneString(this DateTime dt, TimeZoneInfo sourceTimeZone)
TimeZoneInfo eastTz = GetEasternTimeZoneInfo();
return dt.ToTimeZoneString(sourceTimeZone, eastTz);
public static string ToEasternTimeZoneString(this DateOnly dtDo)
var midnight = new TimeOnly(0, 0, 0);
DateTime convertedDt = dtDo.ToDateTime(midnight);
return convertedDt.ToString("MM/dd/yyyy");
public static string ToEasternTimeZoneString(this TimeOnly dtTo, TimeZoneInfo sourceTimeZone)
TimeZoneInfo eastTz = GetEasternTimeZoneInfo();
return dtTo.ToTimeZoneString(sourceTimeZone, eastTz);
public static string ToTimeZoneString(this DateTime dt, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone)
DateTime convertedDt = dt.ConvertToUnspecifiedDateTimeKind();
convertedDt = convertedDt.ConvertBetweenTimeZones(sourceTimeZone, destinationTimeZone);
return convertedDt.ToString("MM/dd/yyyy hh:mm:ss tt");
public static string ToTimeZoneString(this TimeOnly dtTo, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone)
var ts = dtTo.ToTimeSpan();
var dt = DateTime.Today.AddMilliseconds(ts.TotalMilliseconds);
DateTime convertedDt = dt.ConvertToUnspecifiedDateTimeKind();
convertedDt = convertedDt.ConvertBetweenTimeZones(sourceTimeZone, destinationTimeZone);
return convertedDt.ToString("hh:mm:ss tt");
public static DateOnly ToDateOnly(this DateTime dateTime)
return new DateOnly(dateTime.Year, dateTime.Month, dateTime.Day);
public static TimeOnly ToTimeOnly(this DateTime dateTime)
return new TimeOnly(dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond);
private static TimeZoneInfo GetEasternTimeZoneInfo()
TimeZoneInfo tz = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
: TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
private static TimeZoneInfo GetCentralTimeZoneInfo()
TimeZoneInfo tz = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time")
: TimeZoneInfo.FindSystemTimeZoneById("America/Chicago");
private static TimeZoneInfo GetPacificTimeZoneInfo()
TimeZoneInfo tz = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time")
: TimeZoneInfo.FindSystemTimeZoneById("America/Los_Angeles");
private static TimeZoneInfo GetServerTimeZoneInfo()
TimeZoneInfo tz = TimeZoneInfo.Local;
public static DateTime ConvertToUnspecifiedDateTimeKind(this DateTime dt)
return DateTime.SpecifyKind(dt,DateTimeKind.Unspecified);
public static DateTime ConvertBetweenTimeZones(this DateTime dt, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone)
return TimeZoneInfo.ConvertTime(dt, sourceTimeZone, destinationTimeZone);