Results 1 to 10 of 10

Thread: leap-year

  1. #1
    Join Date
    Aug 2002
    Posts
    25

    Unanswered: leap-year

    Anybody knows if there is a function that lets somebody know if a year is a leap-year in VB?

    Thanks in advance.

  2. #2
    Join Date
    Nov 2002
    Location
    Ohio
    Posts
    90
    Hi mvargasp,

    No, there isn't a leap year function in VB. I usually use this

    Private Function LeapYear(tmpDate As Date) As Boolean
    Dim lclYear As Integer
    '
    ' get year from date
    '
    lclYear = Year(tmpDate)
    Select Case lclYear Mod 4
    Case 0 ' might be leap year
    If lclYear Mod 400 = 0 Then
    LeapYear = False
    Else
    LeapYear = True
    End If
    Case Else
    LeapYear = False
    End Select

    End Function

    Good Luck,
    Bruce Baasch

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    you forgot to divide by 100

    1900 was not a leap year

  4. #4
    Join Date
    Nov 2002
    Location
    Ohio
    Posts
    90
    Thanks,

    No...there is not a function or "easy" way to get leap years.
    Bruce Baasch

  5. #5
    Join Date
    Nov 2002
    Location
    Ohio
    Posts
    90
    Thanks,

    No...there is not a function or "easy" way to get leap years.
    Bruce Baasch

  6. #6
    Join Date
    Feb 2003
    Location
    San Antonio, TX
    Posts
    31
    Originally posted by Bruce A. Baasch
    Hi mvargasp,

    No, there isn't a leap year function in VB. I usually use this

    Private Function LeapYear(tmpDate As Date) As Boolean
    Dim lclYear As Integer
    '
    ' get year from date
    '
    lclYear = Year(tmpDate)
    Select Case lclYear Mod 4
    Case 0 ' might be leap year
    If lclYear Mod 400 = 0 Then
    LeapYear = False
    Else
    LeapYear = True
    End If
    Case Else
    LeapYear = False
    End Select

    End Function

    Good Luck,
    Actually, there is a bug in that code. the test should be
    Case 0 ' might be leap year
    If lclYear Mod 400 = 0 Then
    LeapYear = True
    Else
    LeapYear = False
    End If

    Every century that is NOT divisable by 400 is NOT a leap year.
    Ralph D. Wilson II
    email: rwilson@thewizardsguild.com
    URL: http://thewizardsguild.com

    "Any sufficiently advanced technology is indistinguishable from magic." A.C. Clark

  7. #7
    Join Date
    Mar 2004
    Posts
    1

    Red face Date Validation

    Does anyone know how to do the leap year in VB...without using the ISdate and showing a message box if they entered the wrong date. For example, if they entered (mm/dd/yy) 02/29/03 a message box will pop up and say "INVALID DATE"

  8. #8
    Join Date
    Feb 2003
    Location
    San Antonio, TX
    Posts
    31

    Date Validation

    [Does anyone know how to do the leap year in VB...without using the ISdate and showing a message box if they entered the wrong date. For example, if they entered (mm/dd/yy) 02/29/03 a message box will pop up and say "INVALID DATE"]

    Essentially, you have to perform the various tests that are already coded into IsDate.

    If you work with the _text_ in the field, you can extract the MonthPortion, DayPortiuon, and YearPortion by locating the "/" or "-" characters and doing appropriate substring manipulations. (You can also identify invalid dates because of no separators.) Of course, you may have to allow the user to define the month, day, year field order (e.g. MM/DD/YYYY, YYYY-MM-DD, or DD/MM/YY).

    Having performed those extractions, You can validate the YearPortion based upon two or four digit year rules (you'll have to define or allow the user to define the 2 digit year rule).

    I would next validate the MonthPortion as being between 1 and 12, inclusive.

    Based upon the month, you can validate the DayPortion with the only kink being the handling of Leap Years based upon the YearPortion and a MonthPortion of "2" or "02".

    Were you asking for some specific code?
    Ralph D. Wilson II
    email: rwilson@thewizardsguild.com
    URL: http://thewizardsguild.com

    "Any sufficiently advanced technology is indistinguishable from magic." A.C. Clark

  9. #9
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1

    Re: leap-year

    Originally posted by mvargasp
    Anybody knows if there is a function that lets somebody know if a year is a leap-year in VB?

    Thanks in advance.
    Why do you need to do this?

    I can't give a clear answer until I know the application, but I can give you a function that I regularly use to address real-world problems with leap years.
    Actually, I have this implemented as a UDF in SQL Server 2k, but this is a working adaptation for use in VB, it's essentially the exact same.:

    Code:
    Private Function DaysInYear(d As Date) As Integer
       DaysInYear = DateDiff("d", "1/1/" & Str(year(d)), "1/1/" & Str(year(d)) + 1)
    End Function
    You could use this to answer your original question like so:
    Code:
    If DaysInYear(yourValue) = 366 Then
       MsgBox yourValue & " occurs during a leap year."
    End If
    A practical application that I have to use this for on a daily basis is calculating figures based on pro-rated information.

    Also, I was thinking, if you want it to return a yes/no for whether the date is in a leap year, that's a fairly straight-forward modification:

    [code]Private Function IsLeapYear(d As Date) As Boolean
    Dim i As Integer
    i = DateDiff("d", "1/1/" & Str(year(d)), "1/1/" & Str(year(d)) + 1)
    If i = 366 Then
    IsLeapYear = True
    Else
    IsLeapYear = False
    End If
    End Function

    New Usage:

    Code:
    If IsLeapYear(yourValue) Then
       MsgBox yourValue & " is a leap year
    End If
    Last edited by Teddy; 04-02-04 at 14:35.

  10. #10
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1

    Re: Date Validation

    Originally posted by nav66
    Does anyone know how to do the leap year in VB...without using the ISdate and showing a message box if they entered the wrong date. For example, if they entered (mm/dd/yy) 02/29/03 a message box will pop up and say "INVALID DATE"
    This really isn't a function of a leap year, this is a function of whether or not the user entered a valid date. Why wouldn't you use the IsDate function?

    Code:
    If IsDate(yourValue) = False Then
       MsgBox "INVALID DATE"
    End If
    Last edited by Teddy; 04-02-04 at 15:41.

Posting Permissions

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