Check if a date occurs in the current year
Compare the UTC year of a date-time with the current one.
function isCurrentYear({ dateTime, now }) { return new Date(dateTime).getUTCFullYear() === new Date(now).getUTCFullYear(); }
Input & expected output
Input
{
"dateTime": "2026-01-15T00:00:00Z",
"now": "2026-10-28T12:00:00Z"
}Expected output
trueThe edge case
Input
{
"dateTime": "2025-12-31T23:00:00-03:00",
"now": "2026-10-28T12:00:00Z"
}Expected output
trueKnow the limits
- The year is the UTC year: New Year’s Eve at 23:00 in São Paulo is already 2026 in UTC, as the edge case shows.
- now is an input so the result is testable.
The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.
Source & maintenance
Based on Check if a date occurs in the current year from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.
Takes the current time as an explicit input so the result is reproducible. Original MDX retained as inert source evidence.
Maintained by Oveyon. Original source commit: 14b481cf60df. This edition’s file: contents/check-if-a-date-occurs-in-the-current-year.mdx in akamind/1loc, commit 0a77f131b22d. Read the license.
Keep exploring
- Check whether all values are equal — Compare every item with the first using Object.is.
- Check whether an object is empty — Check for own enumerable string keys on a JSON object.
- Check whether an integer is even — Test parity for safe integers, including zero and negative values.