Date is a Leap Year

Hi Team,

I am trying a give a random date and would like to identify whether that year falls into Leap Year or not.

Hi.

IsDate() or DateTime.TryParse() will give you true or false if the string is a date. So, if you take the Year from the date and use it on the month and day, then you can check if it’s a date.

I ran a test for this using this line of code:

IsDate("2/29/"+CStr(Year(CDate("1/1/2020"))))
IsDate("2/29/"+CStr(Year(CDate("1/1/2019"))))

First one was TRUE and second one was FALSE.

Regards.

Year Mod 4 = 0

where Year should be pciked up from your date …

1 Like

We have a Microsoft inbuilt option to check the leap year

DateTime.IsLeapYear(year)

You need to pass year in this from your date.
If you have a string datetime then convert it to datetime and then use the above line of code

2 Likes

Thank you, it worked

While the Mod 4 method may suffice in 99% of cases, you might want to instead use the other two methods suggested if you are dealing with a data set that stretch over a long time. e.g. year 1900 and 2100 are not leap years.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.