Masriyah
Masriyah

Reputation: 2525

setting datetime to null

I have two DateTime variables first period and second period and with each period i have a date range between two date for each period. My issue is that i usually set my dates to null withn i load the page but for these dates i cannot set them to null which i would like rather than setting a specific date.

DateTime firstPeriodBeginDate = DateTime.Today.AddMonths(-3);
DateTime secondPeriodBeginDate = DateTime.Today.AddMonths(-2);

I would like it set to null so that it will equal to the values selected from the date picker by the user when used to render results.

Upvotes: 1

Views: 29264

Answers (2)

dknaack
dknaack

Reputation: 60556

The DateTime structure cant be null.

But you can use a nullable DateTime

DateTime? dateTime;
dateTime = null;

and check for null

if (dateTime.HasValue)

More information: C# Nullable DateTime

hope this helps

Upvotes: 2

Oded
Oded

Reputation: 499352

DateTime is a non-nullable value type, so cannot be null.

You can use a Nullable<DateTime> for such a thing:

DateTime? firstPeriodBeginDate = DateTime.Today.AddMonths(-3);
DateTime? secondPeriodBeginDate = null;

Upvotes: 19

Related Questions