Time.daysInYear
func Time.daysInYear(t: Time.Dated): intTime.daysInYear returns the number of days in the year of t, 365 or 366.
Details
Time.daysInYeartakes any value with a date: aPlainDate, aPlainDateTime, or aZonedDateTime. AnInstanthas no date until you give it a zone withTime.inZone.- The year of a
ZonedDateTimeis the year in its zone, so one instant can fall in years of different lengths in two zones.
Basic examples
Time.daysInYear(Time.plainDate(2024, 3, 1)) // 366
Time.daysInYear(Time.plainDate(2025, 3, 1)) // 365Call it through . on a date:
let release = Time.plainDate(2028, 7, 4)
release.daysInYear() // 366Scope
The answer depends on the year alone, not on the month or day:
Time.plainDate(2024, 1, 1).daysInYear() // 366
Time.plainDate(2024, 12, 31).daysInYear() // 366Of the century years shown, only 2000 has 366 days:
Time.plainDate(1900, 6, 1).daysInYear() // 365
Time.plainDate(2000, 6, 1).daysInYear() // 366
Time.plainDate(2100, 6, 1).daysInYear() // 365Years before 1 work too:
Time.plainDate(0, 6, 1).daysInYear() // 366
Time.plainDate(-1, 6, 1).daysInYear() // 365
Time.plainDate(-4, 6, 1).daysInYear() // 366A PlainDateTime and a ZonedDateTime answer for their date:
let eve = Time.plainDateTime(Time.plainDate(2024, 12, 31), Time.plainTime(hour: 23))
let stockholm = Result.unwrap(Time.zone("Europe/Stockholm"))
eve.daysInYear() // 366
eve.toZoned(stockholm).daysInYear() // 366A value typed as the union Time.Dated passes as it is, with no is test:
func yearLength(t: Time.Dated): string {
"${t.year()}: ${t.daysInYear()} days"
}
println(yearLength(Time.plainDate(2023, 5, 1)))
println(yearLength(Time.plainDateTime(Time.plainDate(2024, 5, 1), Time.plainTime())))2023: 365 days
2024: 366 daysApplications
How far through the year a date is, as a fraction:
func yearProgress(d: Time.PlainDate): float64 {
float64(d.dayOfYear()) / float64(d.daysInYear())
}
println(yearProgress(Time.plainDate(2024, 7, 1)))
println(yearProgress(Time.plainDate(2025, 7, 2)))0.5
0.5013698630136987Days left until the end of the year:
let today = Time.plainDate(2026, 11, 11)
println("${today.daysInYear() - today.dayOfYear()} days left in ${today.year()}")50 days left in 2026Daily interest under an actual/actual convention, where a day is a share of the year it falls in:
func dailyInterest(balance: float64, rate: float64, on: Time.PlainDate): float64 {
balance * rate / float64(on.daysInYear())
}
println(dailyInterest(36600.0, 0.05, Time.plainDate(2024, 3, 1)))
println(dailyInterest(36500.0, 0.05, Time.plainDate(2025, 3, 1)))5.0
5.0Count the days in a span of whole years:
let days = Array.range(2001, 2101)
.map({ Time.plainDate(x, 1, 1).daysInYear() })
.sum()
println(days)36524Properties and relations
A year has 366 days exactly when Time.inLeapYear is true:
for year in Array.range(1896, 1905) {
let d = Time.plainDate(year, 1, 1)
assert((d.daysInYear() == 366) == d.inLeapYear())
}The last day of the year is day daysInYear of it, and the months add up to the year:
for year in [1900, 2000, 2023, 2024] {
let d = Time.plainDate(year, 1, 1)
assert(Time.plainDate(year, 12, 31).dayOfYear() == d.daysInYear())
let months = Array.range(1, 13).map({ Time.plainDate(year, x, 1).daysInMonth() }).sum()
assert(months == d.daysInYear())
}Adding daysInYear days to January 1 gives January 1 of the next year:
let jan1 = Time.plainDate(2024, 1, 1)
let next = jan1.add(Time.duration(days: jan1.daysInYear()))
assert(next == Time.plainDate(2025, 1, 1))Possible issues
An Instant has no year, so passing one is a compile error:
let now = Result.unwrap(Time.parseInstant("2024-12-31T23:30:00Z"))
println(Time.daysInYear(now)) // error: cannot use Time.Instant where Time.Dated is expectedGive it a zone first. The zone decides the year, and with it the answer:
let now = Result.unwrap(Time.parseInstant("2024-12-31T23:30:00Z"))
let utc = Time.inZone(now, Time.utc)
let tokyo = Time.inZone(now, Result.unwrap(Time.zone("Asia/Tokyo")))
println("${utc.year()}: ${utc.daysInYear()}")
println("${tokyo.year()}: ${tokyo.daysInYear()}")2024: 366
2025: 365A PlainTime has no date either:
Time.daysInYear(Time.plainTime(hour: 9)) // error: cannot use Time.PlainTime where Time.Dated is expectedSee also
Time.inLeapYear · Time.daysInMonth · Time.dayOfYear · Time.year · Time.inZone · Time
