如何根据出生日期计算年龄,格式为6/24/1976周一/日期/年...
答案 0 :(得分:258)
这些答案中的许多答案都没有恰当地说明闰年,最好是使用Apple的方法而不是用常数来划分。
let birthday: NSDate = ...
let now = Date()
let ageComponents = calendar.dateComponents([.year], from: birthday, to: now)
let age = ageComponents.year
NSDate* birthday = ...;
NSDate* now = [NSDate date];
NSDateComponents* ageComponents = [[NSCalendar currentCalendar]
components:NSCalendarUnitYear
fromDate:birthday
toDate:now
options:0];
NSInteger age = [ageComponents year];
我认为这比其他任何答案更清晰,更准确。
答案 1 :(得分:10)
以下是根据您的出生日期(以年和日)计算实际年龄的方法:
NSString *birthDate = @"03/31/1990";
NSDate *todayDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
int time = [todayDate timeIntervalSinceDate:[dateFormatter dateFromString:birthDate]];
int allDays = (((time/60)/60)/24);
int days = allDays%365;
int years = (allDays-days)/365;
NSLog(@"You live since %i years and %i days",years,days);
它返回年份和确切的天数。如果需要,您可以更改NSDateFormatter等等。
答案 2 :(得分:6)
这是我从这个link得到的代码..它的效果很好..输出就像“5年6个月”,所有用例都包含在其中。
- (NSString *)age:(NSDate *)dateOfBirth {
NSInteger years;
NSInteger months;
NSInteger days;
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *dateComponentsNow = [calendar components:unitFlags fromDate:[NSDate date]];
NSDateComponents *dateComponentsBirth = [calendar components:unitFlags fromDate:dateOfBirth];
if (([dateComponentsNow month] < [dateComponentsBirth month]) ||
(([dateComponentsNow month] == [dateComponentsBirth month]) && ([dateComponentsNow day] < [dateComponentsBirth day]))) {
years = [dateComponentsNow year] - [dateComponentsBirth year] - 1;
} else {
years = [dateComponentsNow year] - [dateComponentsBirth year];
}
if ([dateComponentsNow year] == [dateComponentsBirth year]) {
months = [dateComponentsNow month] - [dateComponentsBirth month];
} else if ([dateComponentsNow year] > [dateComponentsBirth year] && [dateComponentsNow month] > [dateComponentsBirth month]) {
months = [dateComponentsNow month] - [dateComponentsBirth month];
} else if ([dateComponentsNow year] > [dateComponentsBirth year] && [dateComponentsNow month] < [dateComponentsBirth month]) {
months = [dateComponentsNow month] - [dateComponentsBirth month] + 12;
} else {
months = [dateComponentsNow month] - [dateComponentsBirth month];
}
if ([dateComponentsNow year] == [dateComponentsBirth year] && [dateComponentsNow month] == [dateComponentsBirth month]) {
days = [dateComponentsNow day] - [dateComponentsBirth day];
}
if (years == 0 && months == 0) {
if (days == 1) {
return [NSString stringWithFormat:@"%d day", days];
} else {
return [NSString stringWithFormat:@"%d days", days];
}
} else if (years == 0) {
if (months == 1) {
return [NSString stringWithFormat:@"%d month", months];
} else {
return [NSString stringWithFormat:@"%d months", months];
}
} else if ((years != 0) && (months == 0)) {
if (years == 1) {
return [NSString stringWithFormat:@"%d year", years];
} else {
return [NSString stringWithFormat:@"%d years", years];
}
} else {
if ((years == 1) && (months == 1)) {
return [NSString stringWithFormat:@"%d year and %d month", years, months];
} else if (years == 1) {
return [NSString stringWithFormat:@"%d year and %d months", years, months];
} else if (months == 1) {
return [NSString stringWithFormat:@"%d years and %d month", years, months];
} else {
return [NSString stringWithFormat:@"%d years and %d months", years, months];
}
}
}
如果您希望年龄为int,请尝试以下答案..我从link
开始- (NSInteger)age:(NSDate *)dateOfBirth {
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *dateComponentsNow = [calendar components:unitFlags fromDate:[NSDate date]];
NSDateComponents *dateComponentsBirth = [calendar components:unitFlags fromDate:dateOfBirth];
if (([dateComponentsNow month] < [dateComponentsBirth month]) ||
(([dateComponentsNow month] == [dateComponentsBirth month]) && ([dateComponentsNow day] < [dateComponentsBirth day]))) {
return [dateComponentsNow year] - [dateComponentsBirth year] - 1;
} else {
return [dateComponentsNow year] - [dateComponentsBirth year];
}
}
希望这对某人有所帮助..
答案 3 :(得分:3)
如果您想单独找年龄,请使用下面包含闰年计算的年龄
- (NSInteger)age:(NSDate *)dateOfBirth {
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *dateComponentsNow = [calendar components:unitFlags fromDate:[NSDate date]];
NSDateComponents *dateComponentsBirth = [calendar components:unitFlags fromDate:dateOfBirth];
if (([dateComponentsNow month] < [dateComponentsBirth month]) ||
(([dateComponentsNow month] == [dateComponentsBirth month]) && ([dateComponentsNow day] < [dateComponentsBirth day]))) {
return [dateComponentsNow year] - [dateComponentsBirth year] - 1;
} else {
return [dateComponentsNow year] - [dateComponentsBirth year];
}
}
答案 4 :(得分:3)
一个简单易用的swift扩展来获取NSDate
extension NSDate {
var age: Int {
let calendar: NSCalendar = NSCalendar.currentCalendar()
let now = calendar.startOfDayForDate(NSDate())
let birthdate = calendar.startOfDayForDate(self)
let components = calendar.components(.Year, fromDate: birthdate, toDate: now, options: [])
return components.year
}
}
例:
NSDate(timeIntervalSince1970:0).age // => 45 (as of nov 2015) Epoch happened 45 year ago !
顺便说一句,你应该小心,这是西方的年龄概念,在其他一些国家,计算是不同的(例如Korea)。
答案 5 :(得分:2)
可能我错了,但这更简单,不是吗?
NSTimeInterval ageInterval = [birthDate timeIntervalSinceDate:[NSDate date]];
NSInteger age = ABS(ageInterval / (60 * 60 * 24 * 365));
答案 6 :(得分:0)
最简单的方法是使用NSCalendar:
NSDate *_dateOfBirth = ...;
NSDateComponent *_diff = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:_dateOfBirth toDate:[NSDate date] options:0];
NSLog(@"%@", _diff);
答案 7 :(得分:0)
-(NSInteger) ComputeYearMonth
{
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd"];
NSDate *DateOfBirth=[format dateFromString:[NSString stringWithFormat:@"%@-%@-%@",year,month,day]];
NSDate *currentTime = [NSDate date];
NSLog(@"DateOfBirth======%@",DateOfBirth);
NSInteger years = [[[NSCalendar currentCalendar] components:NSYearCalendarUnit
fromDate:DateOfBirth
toDate:currentTime options:0]year];
NSInteger months = [[[NSCalendar currentCalendar] components:NSMonthCalendarUnit
fromDate:DateOfBirth
toDate:currentTime options:0]month];
NSLog(@"Number of years: %d",years);
NSLog(@"Number of Months: %d,",months);
return years;
// End of Method
}
答案 8 :(得分:0)
这是计算年龄的函数(Swift 2.1)
func calculateAge (birthday: NSDate) -> NSInteger {
let calendar : NSCalendar = NSCalendar.currentCalendar()
let unitFlags : NSCalendarUnit = [NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day]
let dateComponentNow : NSDateComponents = calendar.components(unitFlags, fromDate: NSDate())
let dateComponentBirth : NSDateComponents = calendar.components(unitFlags, fromDate: birthday)
if ( (dateComponentNow.month < dateComponentBirth.month) ||
((dateComponentNow.month == dateComponentBirth.month) && (dateComponentNow.day < dateComponentBirth.day))
)
{
return dateComponentNow.year - dateComponentBirth.year - 1
}
else {
return dateComponentNow.year - dateComponentBirth.year
}
}
答案 9 :(得分:0)
let birthday = NSCalendar.currentCalendar().dateWithEra(1, year: 2016, month: 07, day: 25, hour: 8, minute: 53, second: 0, nanosecond: 0)!
func calculateAge(birthday: NSDate) -> String {
func grammaticalString(forUnit unit: String, value: Int) -> String {
if value > 1 {
return "\(value) \(unit)s"
} else {
return "\(value) \(unit)"
}
}
let calendar = NSCalendar.currentCalendar()
let unitFlags: NSCalendarUnit = [NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute]
let ageComponent = calendar.components(unitFlags, fromDate: birthday, toDate: NSDate(), options: [])
let years = ageComponent.year
let months = ageComponent.month
let days = ageComponent.day
let hours = ageComponent.hour
switch (years, months, days, hours) {
case (let y, let m, _, _) where y > 0:
return "\(grammaticalString(forUnit: "Year", value: y)) \(grammaticalString(forUnit: "Month", value: m))"
case (let y, let m, let d, _) where y == 0 && m > 0:
return "\(grammaticalString(forUnit: "Month", value: m)) \(grammaticalString(forUnit: "Day", value: d))"
case (let y, let m, let d, _) where y == 0 && m == 0 && d > 0:
return "\(grammaticalString(forUnit: "Day", value: d))"
case (let y, let m, let d, let h) where y == 0 && m == 0 && d == 0 && h > 0:
return "\(grammaticalString(forUnit: "Hour", value: h))"
case (let y, let m, let d, let h) where y == 0 && m == 0 && d == 0 && h == 0:
if ageComponent.minute > 0 {
return "\(grammaticalString(forUnit: "Minute", value: ageComponent.minute))"
} else {
return "-"
}
default:
return "-"
}
}
// Output will be any of these
// 2 Years 3 Months
// 3 Months 0 Days
// 2 Days
// 3 Hours
// 45 Minutes
// -
答案 10 :(得分:0)
基于cobbal's solution的 Swift 3及以上语法中的方法。
func calculateAgeInYearsFromDateOfBirth (birthday: Date) -> Int {
let now = Date()
let calendar = Calendar.current
let ageComponents = calendar.dateComponents([.year], from: birthday, to: now)
let age = ageComponents.year!
return age
}