toDateInYear method
Ignores the year of the current DateTime and returns a new DateTime
with the specified setYear.
Requires month and day to be set in the current DateTime.
Returns null if the date is invalid in setYear — for example, when
this is February 29 (a leap day) and setYear is not a leap year.
Audited: 2026-06-12 11:26 EDT
Implementation
@useResult
DateTime? toDateInYear(int setYear) {
if (month == DateTime.february && day == DateConstants.daysInFebLeapYear) {
final bool isTargetLeap = DateTimeUtils.isLeapYear(year: setYear);
if (!isTargetLeap) {
return null;
}
}
// ignore: saropa_lints/avoid_datetime_constructor, saropa_lints/avoid_datetime_constructor_unvalidated -- month/day/time come from this valid DateTime; only the year changes, and Feb 29 into a non-leap year returns null above
return DateTime(
setYear,
month,
day,
hour,
minute,
second,
millisecond,
microsecond,
);
}