-3

I am trying to set int? to null or to the dateTime.Year or a TimeSpan. I am checking if the dt is equal to the default datetime. Then setting the int? to null or a calculated value. Is this the correct way to do this?

DateTime? dtnull = null;
DateTime? dt = new DateTime(2023, 09,27,00,00,00);
DateTime currentDateTime = DateTime.Now;

int? RemainingDaysnull = (dtnull == default(DateTime)) ? null : (dtnull.Value - currentDateTime).Days;
int? Last4EndDatenull = (dtnull == default(DateTime)) ? null(dtnull.Value).Year;
int? RemainingDays = (dt == default(DateTime)) ? null : (dt - currentDateTime).Days;
int? Last4EndDate = (dt == default(DateTime)) ? null : (dt.Value).Year();
Buh Buh
  • 7,443
  • 1
  • 34
  • 61
Jefferson
  • 173
  • 2
  • 12
  • 32

2 Answers2

2

It's not really clear what you are trying to achieve, but here is how you could get check that dt is well populated and then calculate a couple of values when that's true.

int? remainingDays = null;
int? last4EndDate = null;

if (dt.HasValue && dt.Value > DateTime.MinValue)
{
    remainingDays = (dt.Value - currentDateTime).Days;
    last4EndDate = dt.Value.Year;
}
Buh Buh
  • 7,443
  • 1
  • 34
  • 61
  • You could do `if(dt > DateTime.MinValu)` as a comparison involving a `null` `DateTime?` with a `DateTime` will be false – juharr Oct 12 '20 at 16:38
  • @juharr Yea, I know. I'm just trying to keep it baby steps because the requirements aren't exactly clear. – Buh Buh Oct 12 '20 at 16:43
  • @juharr why do I need to set `remainingDays = null` to null? – Jefferson Oct 12 '20 at 17:55
  • @Jefferson Null is just a default value, incase your `if` statement is false. It doesn't have to be null; you can set it to whatever value you need. – Buh Buh Oct 13 '20 at 08:36
1

You need to tell the compiler that the expression should evaluate as an int?. Try either of these:

Instead try

 int? RemainingDaysnull = (dtnull == default(DateTime)) ? (int?)null : (dtnull.Value - currentDateTime).Days;

Or

 int? RemainingDaysnull = (dtnull == default(DateTime)) ? null : (int?)(dtnull.Value - currentDateTime).Days;
GraemeC
  • 101
  • 3
  • You can if the value in the other part of the conditional is a nullable type. For instance you could apply the `(int?)` to the other side and it will work. – juharr Oct 12 '20 at 16:31
  • I created this sample https://dotnetfiddle.net/6BXVWw where I am getting the error – Jefferson Oct 12 '20 at 17:51
  • Within your dotnetfiddle you're initialising dt (line 8) with NULL, then in your comparison on line 11 your firstly checking if it's equal to default(DateTime) which it isn't so it then tries to get the value of (dt dt.value) this is where it will be falling over. Unless dt is being populated with a valid value I can't see what you're trying to achieve with this block of code. – GraemeC Oct 13 '20 at 09:25
  • Change line 8 in your dotnetfiddle to something like - `DateTime? dt = DateTime.Now.AddDays(7);` – GraemeC Oct 13 '20 at 09:28
  • @GraemeC The code would need to both in both case if dt has a value or is null. Why when I set to to `Now.AddDays(7)` it works? – Jefferson Oct 13 '20 at 17:32
  • should it be `int? RemainingDays = (dt == null) ? null : (int?)(dt.Value - currentDateTime).Days;` – Jefferson Oct 13 '20 at 17:40
  • I'd say the safest way would be to set it to `int? RemainingDays = (dt.GetValueOrDefault() == DateTime.MinValue) ? null : (int?)(dt.Value - currentDateTime).Days;` this way on your first comparison the `dt.GetValueOrDefault()` is saying get the value of dt or if this null get the default value of DateTime which is DateTime.MinValue – GraemeC Oct 14 '20 at 08:05