setting datetime to null
28,671
Solution 1
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;
Solution 2
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
Author by
Masriyah
Updated on July 09, 2022Comments
-
Masriyah almost 2 years
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.
-
Jon Skeet over 12 yearsIt's probably worth editing the first part to "
DateTime
is a non-nullable value type, so cannot be null." After all,Nullable<DateTime>
is a value type, too... -
Masriyah over 12 yearsI tried this but my issue in my repository and services i am using a daterange and i am not sure of how to set that to null. I tried setting my date times to null but then i get errors when i build saying i cannot convert date range to null.
-
Oded over 12 years@Amina - Well, how can there be a range between dates if one of the dates is
null
? -
Masriyah over 12 yearswhen setting to DateTime.MinDate does that defult to the min value and pass it as a value to render results? I mean if my min value would be 1/1/0001 and it will render results based on that date and then will it change to whatever i choose from the date picker? Thanks
-
Masriyah over 12 yearsfirst date = 01/20/2010 - 02/20/2010 second date = 02/20/2010 - 03/20/2010 for each of the dates i have a date range which could be a week, month, quarterly, or yearly and then i use a date time.
-
dknaack over 12 yearsSorry, not really clear what you mean. Could you explain it please. thank you
-
Masriyah over 12 yearsGet(DateRange firstPeriodDateRange, DateRange secondPeriodDateRange, int? officeId) { return DataContext.Report_ResultsData(firstPeriodDateRange.DateStart, firstPeriodDateRange.DateEnd, secondPeriodDateRange.DateStart, secondPeriodDateRange.DateEnd, officeId).AsQueryable(); }
-
Oded over 12 years@Amina - I don't understand. Perhaps post a new question, referencing this one.
-
Bengie over 12 yearsTo go along with this: DateTime is a struct(value), so it can't be null. You have to box the struct into an object, which adds overhead any time you access it, but it'll do your job of being null. Another way is to have a default value that one can treat as null, like DateTime.Min, since one would have to check for null before comparing anyway.