Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
How to compare two dates and find the difference in number of days
09/10/18 and 10/03/18

Regards,

What I have tried:

C#


using date function
Posted
Updated 10-Mar-19 8:27am

C#
DateTime dt1 = new DateTime (2018, 10, 9);
DateTime dt2 = new DateTime (2018, 3, 10);
Timespan diff = dt1 - dt2;
Console.WriteLine(diff.TotalDays);
 
Share this answer
 
Comments
MadMyche 8-Mar-19 7:45am    
You'r better than google!
OriginalGriff 8-Mar-19 8:19am    
I suspect Google is quicker (and makes less typos) :laugh:
MadMyche 8-Mar-19 9:16am    
I don't make typos; there is a problem with my keyboard driver evidenced by it's inability to read my typing
You may be interesting in GitHub project, callled Humanizer[^]

C#
DateTime.UtcNow.AddHours(-30).Humanize() // returns: "yesterday"
TimeSpan.FromMilliseconds(3603001).Humanize(3) // returns" "1 hour, 3 seconds, 1 millisecond"
TimeSpan.FromDays(486).Humanize(maxUnit: TimeUnit.Year, precision: 7) // returms: "1 year, 3 months, 29 days" 
 
Share this answer
 
 
Share this answer
 
DateTime EndDate = new DateTime(2018, 10, 9);
DateTime StartDate = new DateTime(2018, 3, 10);


TimeSpan ts = EndDate - StartDate;

// Difference in days.

int differenceInDays_Int = ts.Days; // This is in int
double differenceInDays_Double = ts.TotalDays; // This is in double


Console.WriteLine(differenceInDays_Int);
Console.WriteLine(differenceInDays_Double);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900