【发布时间】:2021-03-27 19:08:02
【问题描述】:
我正在尝试计算从婴儿到成人(例如 8 周大、4 个月大或 22 岁)的任何人的年龄。但是我的计算失败了,我找不到在哪里。如果我在 2020 年 10 月 16 日输入 DOB,它会返回 8 周大,这是正确的。但是,如果我输入 2020 年 10 月 26 日,它会返回 11 周,这是不正确的。非常感谢任何帮助。
年龄函数:
let bornOn = "10/16/2020"
func calculateAge(dob: String, format: String = "MM/dd/yyyy") -> String{
let df = DateFormatter()
df.dateFormat = format
let date = df.date(from: dob)
guard let val = date else{
return ""
}
var years = 0
var months = 0
var days = 0
let cal = Calendar.current
years = cal.component(.year, from: Date()) - cal.component(.year, from: val)
let currentMonth = cal.component(.month, from: Date())
let birthMonth = cal.component(.month, from: val)
months = currentMonth - birthMonth
if months < 0 {
years = years - 1
months = 12 - birthMonth
if cal.component(.day, from: Date()) < cal.component(.day, from: val){
months = months - 1
}
}else if months == 0 && cal.component(.day, from: Date()) < cal.component(.day, from: val){
years = years - 1
months = 11
}
if cal.component(.day, from: Date()) > cal.component(.day, from: val){
days = cal.component(.day, from: Date()) - cal.component(.day, from: val)
}else if cal.component(.day, from: Date()) < cal.component(.day, from: val){
let today = cal.component(.day, from: Date())
let date = cal.date(byAdding: .month, value: -1, to: Date())
days = date!.daysInMonth - cal.component(.day, from: val) + today
}else{
days = 0
if months == 12 {
years = years + 1
months = 0
}
}
print("Years: \(years), Months: \(months), Days: \(days)")
if years > 0{
if years == 1{
return "\(years) year old"
}else{
return "\(years) years old"
}
}else if months > 4{
return "\(months) months old"
}else {
if months >= 1{
let daysleftInMonth = months * 30
print(daysleftInMonth)
days += daysleftInMonth
}
let weeks = days / 7
return "\(weeks) weeks old"
}
}
extension Date{
var daysInMonth:Int{
let calendar = Calendar.current
let dateComponents = DateComponents(year: calendar.component(.year, from: self), month: calendar.component(.month, from: self))
let date = calendar.date(from: dateComponents)!
let range = calendar.range(of: .day, in: .month, for: date)!
let numDays = range.count
return numDays
}
}
calculateAge(dob: bornOn)
【问题讨论】: