Results 1 to 8 of 8

Thread: Leap year validation(vb.net 2005)

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    56

    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

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    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

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    56

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Leap year validation(vb.net 2005)

    You've already been given your answer.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    56

    Re: Leap year validation(vb.net 2005)

    but the result i'm getting is not precise bcos of leap year

  6. #6
    Addicted Member
    Join Date
    Apr 2009
    Location
    Near Dog River (sorta)
    Posts
    160

    Re: Leap year validation(vb.net 2005)

    Quote Originally Posted by yoosufshanij View Post
    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

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Leap year validation(vb.net 2005)

    Quote Originally Posted by yoosufshanij View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width