Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if it is zero datetime in c#

Tags:

c#

datetime

How to check if it is zero datetime in c#? What I mean zero datetime is: 01.01.0001 00:00:00

like image 422
serefbilge Avatar asked May 09 '13 14:05

serefbilge


People also ask

How do I check if a DateTime is null?

Use model. myDate. HasValue. It will return true if date is not null otherwise false.

What is DateTime MinValue in C#?

The value of this constant is equivalent to 00:00:00.0000000 UTC, January 1, 0001, in the Gregorian calendar. MinValue defines the date and time that is assigned to an uninitialized DateTime variable. The following example illustrates this. C# Copy.


2 Answers

You'll want to check against DateTime.MinValue

More info here

like image 172
Daniel Szabo Avatar answered Sep 23 '22 03:09

Daniel Szabo


You should check against default(DateTime)

The value of that is the same as DateTime.MinValue but formally default(T) is better. It is more readable and more in line with default(int) , which is 0 and very different from int.MinValue.

In a generic class, default(T) works for DateTime as it does for all built-in value types.

like image 36
Henk Holterman Avatar answered Sep 21 '22 03:09

Henk Holterman