import Foundation
struct YMD {
let years: Int
let months: Int
let days: Int
}
func splitDays(_ totalDays: Int) -> YMD {
let calendar = Calendar.current
let start = DateComponents(calendar: calendar, year: 1970, month: 1, day: 1).date!
let end = calendar.date(byAdding: .day, value: totalDays, to: start)!
let components = calendar.dateComponents([.year, .month, .day], from: start, to: end)
return YMD(
years: components.year ?? 0,
months: components.month ?? 0,
days: components.day ?? 0
)
}
let r = splitDays(452)
print("\(r.years) years, \(r.months) months, \(r.days) days")
/*
run:
1 years, 2 months, 28 days
*/