# Calculating Number of Days from Date

```Hi,

I am very new to this, I have two dates,

arrivedate

departdate

I need some code to calculate number of days, ie departdate - arrivaldate,

is there a simple way to do this?

Thanks```
 0
jono1978
3/13/2005 4:29:39 PM
📁 asp.net.web-forms
📃 93655 articles.
⭐ 5 followers.

💬 11 Replies
👁️‍🗨️ 578 Views

```This is how we done a timespan for a fitness project.

string weekcheck = "";

string daycheck = "";

TimeSpan tSpan2;

tSpan2 = DateTime.Now.Subtract((DateTime)fEval.dateentered);

weekcheck = Convert.ToString(tSpan2.Days/7);

daycheck = Convert.ToString(tSpan2.Days);

Our solution figured the Days elapsed between DateTime.Now and the date a record was last saved in the fitness evaluation. To use this just substitute DateTime.Now with (DateTime)departdate and change fEval.dateentered to arrivaldate. the string daycheck will of course be the amount of days and weekcheck will be the amount of weeks.

I hope that this helps you,It's not a bug, it's a feature.Brian ParkerCEO, CofounderNeoTekSystemsPhone:(864)770-0473Fax:(864)770-0476www.neoteksystems.com[email protected]```
 0
brianparker
3/13/2005 5:53:18 PM
```Hi jono1978,

Adding and subtracting Dates is very easy with .NET. Using C#, just Add(+) or Subtract(-) DateTime objects. With VisualBasic you use the DateDiff Method. Example:

[Visual Basic]

Response.Write(DateDiff(DateInterval.Day, departdate, arrivedate))

[C#]

TimeSpan diff = arrivedate - departdate;

Response.Write(diff.Days);

The above assumes your "arrivedate" and "departdate" are DateTime objects.

Hope this helps.Thanks,Geoffrey McGill - Product ManagerBasic Date Picker - A Quicker Picker(TM) - ASP.NET Calendar, Date and Time Web Controls```
 0
BasicDatePicker
3/13/2005 10:49:39 PM
```Hi thanks very much for your replies,

I have done the following which happens when the date is changed in depart date:

Dim arriveDate As DateTime

Dim departDate As DateTime

Response.Write(DateDiff(DateInterval.Day, arrivedate, departdate))

But the response.write statment returns 0 what ever dates are put in, any ideas?

John```
 0
jono1978
3/15/2005 11:12:28 AM
```Hi jono1978,

Your code above "Dim arriveDate As DateTime" creates an empty DateTime object. There is nothing to compare, so the result will be nothing(0). You must fill arriveDate and departDate with a value before any value will be returned. Example:

[Visual Basic]

Dim arrivedate As Date = Date.Today.AddDays(7)

Dim departdate As Date = Date.Today

Response.Write(DateDiff(DateInterval.Day, departdate, arrivedate))

Hope this helps.Thanks,Geoffrey McGill - Product ManagerBasic Date Picker - A Quicker Picker(TM) - ASP.NET Calendar, Date and Time Web Controls```
 0
BasicDatePicker
3/15/2005 3:50:33 PM
```Hi,

thanks again for the help. I have tried this but it only returns 7, I understand this is an example above but I'm having difficulty adapting it

I'm thinking its has something to do with the .AddDays(7) bit, what would I need to put in place for it to responsewrite the actual day differnce.

Sorry if this is obvious but still in the very early stages of ASP.net

 0
jono1978
3/15/2005 8:38:04 PM
```
In your original post you said "I have two dates", which implies that they have already been generated somewhere. You need to pass these dates into the DateDiff function. If they aren't DateTime objects already, then you will need to create the objects using the two values.
Steven BeyRecursion: see Recursion```
 0
stevenbey
3/16/2005 11:56:59 AM
```sorry should have been more clear,

I have two text boxes,

arrivedate

departdate

I need to be able to calculate the day difference,

ie:  arrivedate = 01/01/05

departdate = 05/01/05

need result to say 4 days

appologies again if these appear to be really basic stuff  but I'm still trying to get my head around the theory to it all, so really need some code that does the above for the moment,

thanks again for taking the time to help me out

john```
 0
jono1978
3/16/2005 5:14:33 PM
```Hi jono1978,

Try the following:

[Visual Basic]

Dim arrivedate As Date = Date.Parse("01/01/05")

Dim departdate As Date = Date.Parse("05/01/05")

Response.Write(DateDiff(DateInterval.Day, arrivedate, departdate))

Hope this helps.Thanks,Geoffrey McGill - Product ManagerBasic Date Picker - A Quicker Picker(TM) - ASP.NET Calendar, Date and Time Web Controls```
 0
BasicDatePicker
3/16/2005 8:42:13 PM
```Hi there,

thanks again for this code, I'm still having a bit of a problem, I promise I will give up  after this!

The code above returns 4, but if I change the date in departdate.text for example, how do i then get the 4 to change to the new calculated day, ie if i change departdate.text to 06/01/05, how would I get the code to work out 5 days instead of 4, i'm sure there must be a way to do this without having to change the date written in the code, I have messed around with the code a little but still cant work it out.

john```
 0
jono1978
3/17/2005 5:38:11 AM
```
Try the following:    Dim adate As Date = Date.Parse(arrivedate.text)

Dim ddate As Date = Date.Parse(departdate.text)

Response.Write(DateDiff(DateInterval.Day, adate, ddate))You will need to ensure that arrivedate and departdate are the correct date format.
Steven BeyRecursion: see Recursion```
 0
stevenbey
3/17/2005 10:58:34 AM
```Thanks It worked !!

Thanks to both of you for all your help

Cheers

John```
 0
jono1978
3/17/2005 12:33:38 PM