Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if the values in a DateTime are valid?

We have a line of code:

DateTime.Now.AddMinutes(-1);

Which ran at 2:00 on the morning of the 29th of March 2015 in the UK. The returned value was 1:59, however because of the transition to summer time 1:59 didn't actually happen and caused exceptions further down the line.

In my opinion this is a bug in .NET however in lieu of getting a fix to the framework how can we validate that this is a valid DateTime before the following lines throw exceptions?

I'm really looking for an extension method along the lines of.

if(!theDate.IsValidInTimezone(TimeZoneInfo.Local))
{
  // the time is not valid
}
like image 765
Liath Avatar asked Apr 14 '15 13:04

Liath


1 Answers

TimeZoneInfo.IsInvalidTime method could help you here.

like image 50
Sriram Sakthivel Avatar answered Sep 19 '22 00:09

Sriram Sakthivel