0

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?

D Stanley
  • 149,601
  • 11
  • 178
  • 240
ITNovice15
  • 29
  • 3
  • 9

4 Answers4

2

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..

Aram
  • 5,537
  • 2
  • 30
  • 41
2

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);
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
Manali
  • 37
  • 8
0

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`
Muhammad Raheel
  • 341
  • 1
  • 5
  • 9
0

You can directly use the "-" operator to get the difference.

e.g.

System.TimeSpan diff2 = date2 - date3;

You can use the Day property of timespan object to get the difference

Refer this reference link

Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
Amey Khadatkar
  • 414
  • 3
  • 16