Islapyear

volvomann

Active Member
Licensed User
Longtime User
Hello.

Is ther an funtion lik this isleapyear(now.now) from "VB net" in B4a. I won`t to check i a year is leapyear?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Log(IsLeapYear(2011))
   Log(IsLeapYear(2012))
End Sub

Sub IsLeapYear(Year As Int) As Boolean
   Dim df As String
   df = DateTime.DateFormat
   DateTime.DateFormat = "MM/dd/yyyy"
   Try
      DateTime.DateParse("02/29/" & Year)
      DateTime.DateFormat = df
      Return True
   Catch
      Return False
      DateTime.DateFormat = df
   End Try
End Sub
 
Upvote 0

Jost aus Soest

Active Member
Licensed User
Longtime User
This one is much faster: ;)
B4X:
Sub LeapYear(Year As Int) As Boolean
  If Year Mod 400 = 0 Then Return True
  If Year Mod 100 = 0 Then Return False
  If Year Mod 4 = 0 Then Return True
  Return False
End Sub
 
Last edited:
Upvote 0

volvomann

Active Member
Licensed User
Longtime User
I do not understand,
what do I test on (If LeapYear is= true then label1.text=" Leapyear")?
 
Last edited:
Upvote 0

Jost aus Soest

Active Member
Licensed User
Longtime User
what do I test on (If LeapYear is= true then label1.text=" Leapyear")?

I'm not sure that I really understand your question...
A LeapYear depends on a specific year, so you must give that information, e. g.:
B4X:
If LeapYear(2012) Then Label1.Text = "Leapyear"

If you need this information for the current year, just use DateTime.GetYear(DateTime.Now) instead of 2012.
 
Last edited:
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
If the year is divisible by 400 it is not a leap year.

The year 2000 wasn't a leap year

regards Ricky
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
1600 2000 2400 is leapyear butt not 2100 2200 2300 because they can`t be divided by 400

interesting reading....
but I think I will leave the verification of the years 2100, 2200, 2300 and 2400 to someone else :D
 
Upvote 0

Jost aus Soest

Active Member
Licensed User
Longtime User
Upvote 0

moster67

Expert
Licensed User
Longtime User
@jost...

I don't think you understood my stupid joke. I was referring to the dates - far too much in future for me still being around :)
 
Upvote 0
Top