Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
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

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
 
Share this answer
 
Comments
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
...
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
Reply
[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[^]
 
Share this answer
 
Comments
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
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
 
Share this answer
 
Comments
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    
Your opinion is wrong.
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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900