Skip to content

Instantly share code, notes, and snippets.

@luebster
Created March 17, 2023 13:30
Show Gist options
  • Save luebster/22a3fabda0100859fda9fe485ac8f02e to your computer and use it in GitHub Desktop.
Save luebster/22a3fabda0100859fda9fe485ac8f02e to your computer and use it in GitHub Desktop.
Comparing "Now" to local datetimes in C# that makes the most sense to me
// create a DateTime in UTC Time
DateTime _startDate = TimeZoneInfo.ConvertTimeToUtc(Convert.ToDateTime("3/12/2023 20:00:00"), TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
// Use UTC Now to compare UTC datetimes
DateTime _now = DateTime.UtcNow;
bool nowIsBefore = _now < _startDate;
bool nowIsEqual = _now == _startDate;
bool nowIsAfter = _now > _startDate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment