Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nullable datetimes

Tags:

c#

I have 1 datetime field that is not nullable and 1 that is nullable. I can use the following code with the non nullable one :

 c.StartDate.Day.ToString() + "/" + 
 c.StartDate.Month.ToString() + "/" + 
 c.StartDate.Year.ToString()

But when I try to do this with the nullable one, I get the error :

'System.Nullable' does not contain a definition for 'Day' and no extension method 'Day' accepting a first argument of type 'System.Nullable' could be found (are you missing a using directive or an assembly reference?)

How do I get the Day, Month, Year of a nullable datetime?

like image 590
user517406 Avatar asked May 25 '11 13:05

user517406


People also ask

Should DateTime be nullable?

DateTime itself is a value type. It cannot be null. Show activity on this post. No -- DateTime is a struct in C# and structs (value types) can not be null.

Is DateTime nullable SQL?

Yes. If you have a nullable datetime column, then its fine.

Can DateTime be null in MySQL?

MySQL DOES accept null values for the datetime definition, but if you for some reason think otherwise and won't use a null value, consider simply using '1000-01-01' as the value and excluding rows which have that value for your bill_date column in your queries. Mysql does allow nulls in datetime fields.


1 Answers

You'll have to use the .Value property on your nullable:

c.StartDate.Value.Day.ToString()  //etc

After checking for null, you could:

 c.StartDate.Value.ToString("dd-MMM-yyyy");
like image 193
p.campbell Avatar answered Sep 23 '22 03:09

p.campbell