15,936,821 members
1.00/5 (1 vote)
See more:
calculate number of days difference between two dates in vb.net using timespan

What I have tried:

```Dim d1 As DateTime = "01-Sep-2017"
Dim d2 As DateTime = "30-Sep-2017"
Dim TTF As New TimeSpan
TTF = d2.Subtract(d1)
TTF.TotalDays
TotalDays is 29 days```

```Dim d1 As DateTime = "15-Sep-2017"
Dim d2 As DateTime = "30-Sep-2017"
Dim TTF As New TimeSpan
TTF = d2.Subtract(d1)
TTF.TotalDays
TotalDays is 15 days```

`Why difference between  "01-Sep-2017" and "30-Sep-2017" 29 days not 30 days because difference between "15-Sep-2017" and "30-Sep-2017" is 15 days. i want number of days between two days.`
Posted
Updated 10-Oct-17 1:24am
v2

## Solution 1

Quote:
Why difference between "01-Sep-2017" and "30-Sep-2017" is 30 days

Um. It isn't, according to your example:
VB
```Dim d1 As DateTime = "01-Sep-2017"
Dim d2 As DateTime = "30-Sep-2017"
Dim TTF As New TimeSpan
TTF = d2.Subtract(d1)
TTF.TotalDays
TotalDays is 29 days```

Member 9803671 10-Oct-17 4:22am
sorry for for my question last line it is wrong.
difference between "01-Sep-2017" and "30-Sep-2017" is 29 days.
but why not 30 days.

because difference between "15-Sep-2017" and "30-Sep-2017" is 15 days.

i want total number of days between two dates.
OriginalGriff 10-Oct-17 4:36am
And that is what you get.
30 - 15 == 15
30 - 1 == 29.
Why would you expect it to be any different?
If you want the number of days in a month you need to subtract "first of month A" from "first of month A + 1" not "first of month A" from "last of month A"
Member 9803671 10-Oct-17 4:43am
i want total number of days between any two dates
OriginalGriff 10-Oct-17 4:50am
And that is exactly what you are getting!
Mathematics does not change because it's a date!
2 - 1 == 1
3 - 1 == 2
30 - 15 == 15
...

## Solution 3

If i understand you well, you want to include the day, which ends time period. So, you have to add 1 to the result.
OriginalGriff has already mentioned that (but in other words) in the comment to his answer.
Quote:
OriginalGriff 2hrs 10mins ago
[Modify the comment.] [Delete the comment.]
And that is what you get.
30 - 15 == 15
30 - 1 == 29.
Why would you expect it to be any different?
If you want the number of days in a month you need to subtract "first of month A" from "first of month A + 1" not "first of month A" from "last of month A"

Check this:
VB
```Dim d1 As DateTime = "01-Sep-2017"
Dim d2 As DateTime = "30-Sep-2017"

Dim result = (d2-d1).TotalDays + 1

Console.WriteLine("The difference between date: {0} and {1} is {2} days.", d1, d2, result)```

I strongly suggest to read this: Performing arithmetic operations with dates and times | Microsoft Docs[^]

Ralf Meier 10-Oct-17 9:57am
Hi Maciej,
the problem here is, that (for the OP) the Timespans are different calculated. He thinks, that the difference between "15-Sep-2017" and "30-Sep-2017" is 15 days (which is calculated by Date.Substract) and the difference between 01-Sep-2017 and 30-Sep-2017 is 30 days (which is NOT calculated like this by Date.Substract).
But you are definately right (as I mentioned above as comment to the comment to Solution of Atlapure Ambrish) with your calculation and suggestion.
Maciej Los 10-Oct-17 11:23am
Thank you, Ralf.
Cheers,
Maciej

## Solution 2

The code returns 29 total days, can you share more details where you are seeing 30 days?

```Dim d1 As DateTime = "01-Sep-2017"
Dim d2 As DateTime = "30-Sep-2017"
Dim TTF As New TimeSpan
TTF = d2.Subtract(d1)
TTF.TotalDays```

Member 9803671 10-Oct-17 4:22am
sorry for for my question last line it is wrong.
difference between "01-Sep-2017" and "30-Sep-2017" is 29 days.
but why not 30 days.

because difference between "15-Sep-2017" and "30-Sep-2017" is 15 days.

i want total number of days between two dates.
Ralf Meier 10-Oct-17 5:54am
If the difference (for you) between 01-Sep-2017 and 30-Sep-2017 is 30 days then the difference between 15-Sep-2017 and 30-Sep-2017 must be 16 days.
So the calculation is very easy - you can allways add 1 days to get the result you want (or need).
Atlapure Ambrish 11-Oct-17 2:51am
The way date.subtract is designed, it is giving you correct result, but it seems you want to include the starting day as well. In this case you just need to add 1 to the result returned by date.Subtract method.