|
-
Jun 4th, 2009, 04:09 AM
#1
Thread Starter
Member
Leap year validation(vb.net 2005)
how do i get total no of days between two days which might include leap year
right now i'm getting the no of days by using DateDiff function in vb.net.
but i reckon it is not precise. cos it is not validating leapyear.
pls advise me the best solution for the above
its very urgent
-
Jun 4th, 2009, 04:34 AM
#2
Re: Leap year validation(vb.net 2005)
How about this one
Code:
Dim startDate As Date = "29/05/2009"
Dim endDate As Date = "02/06/2009"
Dim DaysBetweenDate As Integer = (endDate - startDate).Days
Please mark you thread resolved using the Thread Tools as shown
-
Jun 4th, 2009, 06:48 AM
#3
Thread Starter
Member
Re: Leap year validation(vb.net 2005)
i'm getting the no of days between two days by using a vb function like
DateDiff(DateInterval.Day, fromDate, toDate)
but mi problem is of leap year
i want to handle that
pls advise me how to handle leap year in this datedifference
-
Jun 4th, 2009, 07:18 AM
#4
Re: Leap year validation(vb.net 2005)
You've already been given your answer.
-
Jun 4th, 2009, 08:18 AM
#5
Thread Starter
Member
Re: Leap year validation(vb.net 2005)
but the result i'm getting is not precise bcos of leap year
-
Jun 4th, 2009, 08:23 AM
#6
Addicted Member
Re: Leap year validation(vb.net 2005)
 Originally Posted by yoosufshanij
but the result i'm getting is not precise bcos of leap year
What are your two dates?
PC #1: Athlon64 X2+Vista64+VS2008EE+.NET3.5 #2: XP (all flavours Ghost'd)+VS2008EE+.NET3.5
Help Files: HelpMaker · Dr. Explain · Create Icons: IcoFX
"Whoever eats my flesh and drinks my blood has eternal life, and I will raise him on the last day." John 6:54
-
Jun 4th, 2009, 09:25 AM
#7
Re: Leap year validation(vb.net 2005)
 Originally Posted by yoosufshanij
but the result i'm getting is not precise bcos of leap year
I'm curious as to how you arrived that the result is "imprecise"....
What are you getting that you aren't? What are you not getting that you expect? What code are you using to determine that it isn't right?
-tg
-
Jun 4th, 2009, 07:38 PM
#8
Re: Leap year validation(vb.net 2005)
The calculation provided works exactly as it should. Try running the following code in a new console application project:
Code:
Dim d1 As Date
Dim d2 As Date
d1 = #7/1/1899#
d2 = #7/1/1900#
Console.WriteLine((d2 - d1).Days)
d1 = #7/1/1999#
d2 = #7/1/2000#
Console.WriteLine((d2 - d1).Days)
d1 = #7/1/2001#
d2 = #7/1/2002#
Console.WriteLine((d2 - d1).Days)
d1 = #7/1/2003#
d2 = #7/1/2004#
Console.WriteLine((d2 - d1).Days)
Console.ReadLine()
As you can see, 2000 and 2004 are correctly picked as leap years while 1900 and 2002 are correctly picked as not leap years.
It's my guess that it's your understanding of the calculation that is wrong. Consider this:
Code:
Dim d1 As Date = #1/1/2000#
Dim d2 As Date = #1/2/2000#
Console.WriteLine((d2 - d1).Days)
Console.ReadLine()
What would you expect that to display? I'm guessing that you would expect 2, while it actually displays 1. That's because it's calculating the amount of time between 12 AM on the first day and 12 AM on the second day. That is 24 hours, which is 1 day. You need to take that into account when doing your calculations. If you want the number of days in a date range INCLUSIVE of the two dates you specify then you'll have to add 1 to the result of that calculation.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|