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?
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.
Yes. If you have a nullable datetime column, then its fine.
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With