find the number of days between C # dates


This question already has an answer here:

  • Calculate difference between two dates (number of days)? 15 answers

I have a string that coming in date format date=08/21/2016 what I want to do is find the numbers of days From today's date in C# I have converted the string like this.

 DateTime Date = Convert.ToDayTime("08/21/2016");
 DateTime TodayDate = DateTime.Today.Day;
 DateTime SubDate = Date.Subtract(TodayDate);

On the third line, I'm getting error cannot convert System.TimeSpan to System.DateTime

Is there an another way to do this what I want to do I just calculate numbers of days from a future date that is coming as a string


 Answer 19 days

I'm new to C# if it's not to much trouble could somebody share code solution how to achieve this? Thanks for the help

There is no method called Convert.ToDayTime in the Convert class it should be Convert.ToDateTime.

The DateTime allows you to subtract its object from another object of the same type. then You can make use of the .TotalDays function to get the number of days. between those dates.

Use something like this:

DateTime futurDate = Convert.ToDateTime("08/21/2016");
DateTime TodayDate = DateTime.Now;
var numberOfDays = (futurDate - TodayDate).TotalDays;