Datetime "Null" Value

DateTime null value

For normal DateTimes, if you don't initialize them at all then they will match DateTime.MinValue, because it is a value type rather than a reference type.

You can also use a nullable DateTime, like this:

DateTime? MyNullableDate;

Or the longer form:

Nullable<DateTime> MyNullableDate;

And, finally, there's a built in way to reference the default of any type. This returns null for reference types, but for our DateTime example it will return the same as DateTime.MinValue:

default(DateTime)

or, in more recent versions of C#,

default

How to set DateTime to null

It looks like you just want:

eventCustom.DateTimeEnd = string.IsNullOrWhiteSpace(dateTimeEnd)
? (DateTime?) null
: DateTime.Parse(dateTimeEnd);

Note that this will throw an exception if dateTimeEnd isn't a valid date.

An alternative would be:

DateTime validValue;
eventCustom.DateTimeEnd = DateTime.TryParse(dateTimeEnd, out validValue)
? validValue
: (DateTime?) null;

That will now set the result to null if dateTimeEnd isn't valid. Note that TryParse handles null as an input with no problems.

Updating a nullable DateTime field as null results in default DateTime value (0001-01-01 00:00:00.0000000)

I suppose if all else fails, you can simplify most of your code by re-implementing the property:

public class Asset : AggregateRoot<long>
{
public DateTime? _LastControlTime;
[DataType(DataType.DateTime)]
public DateTime? LastControlTime {
get {
return _LastControlTime;
}
set {
if (value == DateTime.MinValue) {
_LastControlTime = null;
} else {
_LastControlTime = value;
}
}
}

It doesn't really cut to the heart of the problem, but will let you progress without having to change all of your == null and .HasValue throughout the entire program.

Can a DateTime be null?

DateTime is a value type, which, just like int and double, has no meaningful null value.

In VB.NET, you can write this:

Dim d As DateTime = Nothing

But all this does is to set d to the default DateTime value. In C# the equivalent code would be this:

DateTime d = default(DateTime);

...which is equivalent to DateTime.MinValue.

That said, there is the Nullable<T> type, which is used to provide a null value for any value type T. The shorthand for Nullable<DateTime> in C# is DateTime?.

Assigning `null` value to Nullable DateTime with single line 'if'

You need to cast null to DateTime?:

oMyClass.ApplicationDate = dr["ApplDate"] == DBNull.Value 
? (DateTime?)null
: Convert.ToDateTime(dr["AppDate"]);

This is because of the way the compiler determines the resulting type of the conditional operator; the behavior is by design:

Either the type of first_expression and second_expression must be the
same, or an implicit conversion must exist from one type to the other.

Since null by itself is of null type and thus there is no conversion from or to it, you need to help the compiler by casting.

Passing Null Value of DateTime of Asp.Net control in C#

Your property should be marked as

 DateTime? dtmActionTaken,

You should check if the textbox has text in it first:

string.IsNullOrEmpty(txtDateAction.Text) ? (DateTime?)null : Convert.ToDateTime(txtDateAction.Text),

You should be testing the following scenario too: If the textbox has text but is not a valid date,



Related Topics



Leave a reply



Submit