Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a DateTime field is not null or empty? [duplicate]

Tags:

c#

I am very new in C# and I have a doubt.

In an application on which I am working I found something like it in the code:

if (!String.IsNullOrEmpty(u.nome)) 

This code simply check if the nome field of the u object is not an empty\null string.

Ok this is very clear for me, but what can I do to check it if a field is not a string but is DateTime object?

like image 568
AndreaNobili Avatar asked Feb 20 '14 11:02

AndreaNobili


People also ask

How check date is null in SQL?

SELECT * FROM employe WHERE date IS NOT NULL AND (date > ? AND date < ?) We first check if the date is null. If this condition its true, then it will check if the date is inside the range.

How do you check if a date is empty?

Use the value property to check if an input type="date" is empty, e.g. if (! dateInput. value) {} . The value property stores the date formatted as YYYY-MM-DD or has an empty value if the user hasn't selected a date.

Can DateTime be null?

DateTime itself is a value type. It cannot be null.

How can I pass an empty string value to a date field in Java?

Try setting the value of the date to 0000-00-00, this usually works (never tried it in this scenario but everywhere else it always worked for me). @AlonAlexander ah if only.


1 Answers

If you declare a DateTime, then the default value is DateTime.MinValue, and hence you have to check it like this:

DateTime dat = new DateTime();   if (dat==DateTime.MinValue)  {      //unassigned  } 

If the DateTime is nullable, well that's a different story:

 DateTime? dat = null;   if (!dat.HasValue)  {      //unassigned  } 
like image 144
Fabian Bigler Avatar answered Sep 20 '22 07:09

Fabian Bigler