If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > leap-year

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-23-03, 13:00
mvargasp mvargasp is offline
Registered User
 
Join Date: Aug 2002
Posts: 25
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.
Reply With Quote
  #2 (permalink)  
Old 01-23-03, 14:55
Bruce A. Baasch Bruce A. Baasch is offline
Registered User
 
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
Reply With Quote
  #3 (permalink)  
Old 01-23-03, 16:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
you forgot to divide by 100

1900 was not a leap year
Reply With Quote
  #4 (permalink)  
Old 01-27-03, 13:25
Bruce A. Baasch Bruce A. Baasch is offline
Registered User
 
Join Date: Nov 2002
Location: Ohio
Posts: 90
Thanks,

No...there is not a function or "easy" way to get leap years.
__________________
Bruce Baasch
Reply With Quote
  #5 (permalink)  
Old 01-27-03, 13:25
Bruce A. Baasch Bruce A. Baasch is offline
Registered User
 
Join Date: Nov 2002
Location: Ohio
Posts: 90
Thanks,

No...there is not a function or "easy" way to get leap years.
__________________
Bruce Baasch
Reply With Quote
  #6 (permalink)  
Old 02-24-03, 18:49
RDWilson2 RDWilson2 is offline
Registered User
 
Join Date: Feb 2003
Location: San Antonio, TX
Posts: 31
Quote:
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
Reply With Quote
  #7 (permalink)  
Old 03-23-04, 16:20
nav66 nav66 is offline
Registered User
 
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"
Reply With Quote
  #8 (permalink)  
Old 03-30-04, 12:40
RDWilson2 RDWilson2 is offline
Registered User
 
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
Reply With Quote
  #9 (permalink)  
Old 04-02-04, 13:21
Teddy Teddy is offline
Purveyor of Discontent
 
Join Date: Mar 2003
Location: The Bottom of The Barrel
Posts: 6,075
Re: leap-year

Quote:
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 13:35.
Reply With Quote
  #10 (permalink)  
Old 04-02-04, 13:49
Teddy Teddy is offline
Purveyor of Discontent
 
Join Date: Mar 2003
Location: The Bottom of The Barrel
Posts: 6,075
Re: Date Validation

Quote:
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 14:41.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On