19
Jan

Local DateTime Utility Function

Just wanted to share a quick utility function. We have a server in another timezone and this frequently presents issues in our client apps when displaying time stamps. There isn’t really a solid solution to handle this in the database so I put together this little utility function that can be called rather than DateTime.Now to get the current local time.

public static DateTime CurrentDateTime()
{
   return TimeZoneInfo.ConvertTime(TimeZoneInfo.ConvertTimeToUtc(DateTime.Now,
                       TimeZoneInfo.Local),
                       TimeZoneInfo.Utc,
                       TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
}

I have this in a utility class called AppLogic so all I need to do is call AppLogic.CurrentDateTime() to get the local EST time. Obviously, you would change your time zone where applicable.

Enjoy!