Blog
Dates - How to check if a year is a leap year
Working with dates, it can sometimes be necessary to work out whether a given year is a leap year. This post describes a technique we can use to carry out this task.
How to determine if a year is a leap year
A technique we can use to determine whether a year is a leap year is to attempt to call the Date function to define a date value for the target year with a day and month of 29th February.
The interesting behaviour is this. If 29th February doesn't exist for the target year, the Date function returns the 1st March for the target year instead (as highlighted in the screenshot beneath).
Because of this behaviour, we can then call the Month function against this return value. If the month value equals 2, this verifies that the input year is a leap year. If the month value equals 3, the year is not a leap year.
As a demonstration, the formula beneath returns false because 2021 is not a leap year.
Month(Date(2021,2,29))=2
Creating a list of years with the leap year status
To provide a further example of how to apply this technique, we can create a list of years and include a field that indicates whether the year is a leap year.
The formula beneath creates a sequence of years starting with the year 1900. For each year in this sequence, we use our technique to add a field that indicates whether we can correctly declare a date value with the day and month of 29th February.
ForAll(Sequence(200,1900,1),
{
YearValue: Year(Date(Value,2,29)),
IsLeapYear: Month(Date(Value,2,29))=2 }
)
Create a list of leap years
To create a list of leap years, we can filter our example from above to return only those records where the IsLeapYear field equals true.
Filter(
ForAll(Sequence(200,1900,1),
{
YearValue: Year(Date(Value,2,29)),
IsLeapYear: Month(Date(Value,2,29))=2
}
),
IsLeapYear
)
Conclusion
- Categories:
- dates
- Dates - How to convert a month name (eg January, Feburary, March) to month number (eg, 1, 2, 3)
- Dates - How to format date day numbers with suffix (st nd rd th)
- Formula - How to get the dates for the current week
- Dates - How to calculate UK tax weeks
- Dates - How to convert dates from MM DD YYYY to DD MM YYYY and vice versa
- Dates - How to detect overlapping times / scheduling conflicts
- Dates - How to display or convert Excel date serial numbers
- Dates - How to get the start date from week number
- Dates - How to get the week number for a given date
- Dates - How to get the nth weekday for a month
- Dates - How to get the last weekday for a month
- Dates - How to get a numbered list of days or months, starting from a specified day or month
- Controls - How to display dates in a combo box
- Search - How to filter records by month and year using a delegable expression