Title: Subtracting dates in ASP.NET with C#
Tags: c#;asp.net
Question: I'm working on subtracting dates in a web form in visual studio 2012 using a begin and end date - I want to find the day difference.
I have two separate web forms that I have connected.
DateTime ArrivalDateCalc = Convert.ToDateTime(.Text)
is what I have tried to use, but it does not work.
Any clues or guidance?
Comment: Possible duplicate of: http://stackoverflow.com/questions/1607336/calculate-difference-between-two-dates-number-of-days

Comment: Can you show your HTML? .Text in Convert.ToDateTime(.Text) seems to be incorrect.

Comment: Can you be more specific on "does not work"?  Do you get an exception?  Is the value wrong?


Here is another answer: You can directly use the "-" operator to get the difference.
System.TimeSpan diff2 = date2 - date3;
You can use the Day property of timespan object to get the difference
Refer this reference link

Here is another answer: You can use .Subtract to subtract dates in C#:
System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
// diff1 gets 185 days, 14 hours, and 47 minutes.
System.TimeSpan diff1 = date2.Subtract(date1);
The result is TimeSpan, which you can get Day, Year etc from it..

Here is another answer: You can Subtract two dates to find out the days difference.
string sdate = "2018-08-25";
string edate = "2018-08-23";
double days = (Convert.ToDateTime(sdate) - Convert.ToDateTime(edate)).TotalDays;
Response.Write("Difference = " + days);

Here is another answer: Although the question is very unclear, but assuming you are using some format;
string startDateInText = "01/01/2014";
string endDateInText = "31/12/2014";
System.DateTime startDate = DateTime.ParseExact(startDateInText, "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture);
System.DateTime endDate = DateTime.ParseExact(endDateInText , "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture);
System.TimeSpan diff = endDate.Subtract(startDate);
//diff.Days will provide difference in Days`

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:10/02/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><asp.net>', 'Title': 'Subtracting dates in ASP.NET with C#', 'ViewCount': 5136, 'id': 32900147, 'user_AboutMe': None, 'user_AccountId': 6085345.0, 'user_DisplayName': 'ITNovice15', 'user_Reputation': 29.0, 'user_Views': 11.0, 'user_id': 4749614.0}}