Time.dayOfYear
func Time.dayOfYear(t: Time.Dated): intTime.dayOfYear returns the day of the year of t, 1 for January 1 to 365, or 366 in a leap year.
Details
- The result runs from 1 to 365, or to 366 in a leap year, where every date from March 1 on is one higher.
- A
Time.ZonedDateTimecounts the date its zone shows, not the date in UTC.
Basic examples
January 1 is day 1:
Time.dayOfYear(Time.plainDate(2026, 1, 1)) // 1December 31 is day 365, or 366 in a leap year:
Time.dayOfYear(Time.plainDate(2026, 12, 31)) // 365
Time.dayOfYear(Time.plainDate(2024, 12, 31)) // 366Scope
A PlainDateTime counts its date and ignores the time:
let start = Time.plainDateTime(Time.plainDate(2026, 2, 1), Time.plainTime(hour: 9, minute: 15))
start.dayOfYear() // 32A ZonedDateTime counts the date of its own zone:
let ny = Result.unwrap(Time.zone("America/New_York"))
let eve = Time.plainDateTime(Time.plainDate(2026, 12, 31), Time.plainTime(hour: 23)).toZoned(ny)
eve.dayOfYear() // 365
eve.withZone(Time.utc).dayOfYear() // 1Pass it as a function value to map a parsed date:
Time.parsePlainDate("2026-07-04") | Result.map(Time.dayOfYear) // Ok{ value: 185 }A negative year counts the same way:
Time.plainDate(-1, 12, 31).dayOfYear() // 365Applications
Count the days left in the year:
let today = Time.plainDate(2026, 11, 11)
today.daysInYear() - today.dayOfYear() // 50Show how far through the year a date is, as a percentage:
let today = Time.plainDate(2026, 11, 11)
today.dayOfYear() * 100 / today.daysInYear() // 86Turn a year and a day number back into a date by adding days to January 1. Without the guard, a day number past the end of the year would roll into the next year:
func fromDayOfYear(year: int, n: int): Time.PlainDate {
let jan1 = Time.plainDate(year, 1, 1)
if n < 1 || n > jan1.daysInYear() {
panic("day ${n} is not in ${year}")
}
jan1.add(Time.duration(days: n - 1))
}
println(fromDayOfYear(2024, 60))
println(fromDayOfYear(2026, 60))2024-02-29
2026-03-01Properties and relations
The last day of the year is daysInYear:
let t = Time.plainDate(2024, 12, 31)
assert(t.dayOfYear() == t.daysInYear())The day of the year is one more than the days since January 1:
let t = Time.plainDate(2026, 11, 11)
let jan1 = t.with(month: 1, day: 1)
assert(t.dayOfYear() == jan1.until(t).part(Time.Day{}) + 1)The day before January 1 is the last day of the previous year:
let jan1 = Time.plainDate(2025, 1, 1)
let before = jan1.subtract(Time.duration(days: 1))
assert(before.dayOfYear() == before.daysInYear())Possible issues
An Instant has no date, so it does not compile. Put it in a zone first:
let epoch = Time.fromEpochMillis(0)
println(Time.dayOfYear(epoch))
// error: cannot use Time.Instant where Time.Dated is expectedTime.fromEpochMillis(0).inZone(Time.utc).dayOfYear() // 1A PlainTime has no date either:
let alarm = Time.plainTime(hour: 7)
println(Time.dayOfYear(alarm))
// error: cannot use Time.PlainTime where Time.Dated is expectedThe same calendar date has a different number in a leap year, so day numbers of different years do not name the same date:
Time.plainDate(2024, 3, 1).dayOfYear() // 61
Time.plainDate(2026, 3, 1).dayOfYear() // 60See also
Time.day · Time.dayOfWeek · Time.weekOfYear · Time.daysInYear · Time.inLeapYear · Time.until · Time.add · Time.PlainDate
