C#: How to find difference of days between 2 nullable datetimes? [duplicate] - c#

This question already has answers here:
C# - Get date difference of Nullable DateTime in int
(2 answers)
How to subtract 2 nullable DateTimes in C#? [closed]
(3 answers)
Closed 3 years ago.
A similar question was posted here in regards to calculating the difference in days of 2 datetimes, however both of my datetimes are nullable, which prevents me from using "TotalDays" as is suggested in that question.
DateTime? startDate;
DateTime? endDate;
return(d1-d2).TotalDays; //This won't work
Any idea how to fix this?
Thanks

You can do this:
if (startDate.HasValue && endDate.HasValue)
{
return (startDate.Value - endDate.Value).TotalDays;
}
else
{
// handle one or more dates being null
}

If the value it's null that will throw an exeption,
to prevent this just add a "?" like this:
return(d1-d2)?.TotalDays;
So if the object is null it won't go any further

Related

Why is this throwing System.FormatException:String was not recognized as a valid DateTime [duplicate]

This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 5 years ago.
Why does this:
Convert.ToDateTime("08/31/2017")
throw an System.FormatException but not this:
Convert.ToDateTime("09/12/2017")
If you need more information please ask and I will update or comment. I have no clue what is causing this issue, so I don't know what details you need.
The default order for this date format on your computers culture is Day/Month/Year, as 31 is not a valid month it fails. If you want this order, you need to provide the format with it:
var x = DateTime.ParseExact("08/31/2017", "MM/dd/yyyy",CultureInfo.InvariantCulture);

DateTime CompareTo operation throwing error [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 5 years ago.
I am trying to compare current date with a calculated date from offset as shown below. The line throws an exception, enters catch block but exception is null. I cannot find what is the exception because of it
DateTime.UtcNow.Date.CompareTo(new UtcDateTime(startDate.AddDays(activity.DueInDaysOffset.GetValueOrDefault()))) > 0
startDate is date from DB
activity.DueInDaysOffset is an integer - positive/negative.
UtcDateTime is a class from 'Microsoft.CommonDataService' used for conversion.
What could be the issue?
Without knowing the rest of your code, I would guess that either startDate or activity is null. Verify that both variables have values.

Why is this a valid comparison [duplicate]

This question already has answers here:
C# okay with comparing value types to null
(11 answers)
Closed 7 years ago.
Here is some example code:
static DateTime time;
if (time == null)
{
/* do something */
}
Since DateTime cannot be null, why does this code compile?
Edit:
The issue is not just that this code will always return false,but why something like DateTime which is never null is allowed in such a comparison.
Although time is of a non-nullable value type, it can be converted to nullable and compared to null. The comparison would yield false, which is a valid outcome.
This does not mean, however, that it is a good code. Tools, such as re:sharper, would flag this line with a warning saying "Expression is always false".

AddYear method doesn't work as excpected [duplicate]

This question already has an answer here:
Add timespan to another timespan does not work [duplicate]
(1 answer)
Closed 8 years ago.
I need to add years to a DateTime variable, but for some reason this function doesn't affect at all..
This is my code:
DateTime dtCmp = new DateTime(calendarBirthDay.Value.Year, calendarBirthDay.Value.Month, calendarBirthDay.Value.Day);
dtCmp.AddYears(DateTime.Now.Year - calendarBirthDay.Value.Year);
I tried even something trivial likewise :
dtCmp.AddYears(1);
and this is doesn't work yet..
You need to store it back to the variable to reflect the change.
dtCmp = dtCmp.AddYears(1);
AddYears method returns a new instance of DateTime object because DateTime is a struct. You need to store returned value into itself to increase old value:
dtCmp = dtCmp.AddYears(1)

DateTime? to string yyyy-mm-dd [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 8 years ago.
I have a method that takes a parameter of the type DateTime?
I need to convert what I receive to a string of the format - yyyy-mm-dd
What is the cleanest optimal way to achieve this?
Assuming dateParameter if of type DateTime, just like you said:
dateParameter.ToString("yyyy-mm-dd");
EDIT:
Since you edited your post to nullable DateTime, here's an edit:
dateParameter.Value.ToString("yyyy-mm-dd");
You can use DateTime.ToString ..below is the sample code :-
datePassed.ToString("yyyy-mm-dd");
And for nullable DateTime :-
datePassed.Value.ToString("yyyy-mm-dd");

Categories