1椿疗、將字符串轉(zhuǎn)成日期,再將日期轉(zhuǎn)成時間戳
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"yyy年MM月dd日"];
NSDate* inputDate = [inputFormatter dateFromString:dateString];
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[inputDate timeIntervalSince1970]];
2糠悼、將時間戳轉(zhuǎn)成時間 再轉(zhuǎn)成字符串
NSTimeInterval time = [model.birthday doubleValue];
NSDate *detailDate = [NSDate dateWithTimeIntervalSince1970:time]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy年MM月dd日"];
NSString *currentTime = [dateFormatter stringFromDate:detailDate];
3届榄、根據(jù)生日時間戳來計算年齡
NSTimeInterval time = [model.birthday doubleValue];
NSDate *detailDate = [NSDate dateWithTimeIntervalSince1970:time];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy年MM月dd日"];
NSString *currentTime = [dateFormatter stringFromDate:detailDate];
_brithdayLabel.text = currentTime;
NSDate *nowDate = [NSDate date];
CGFloat duringTime = [nowDate timeIntervalSinceDate:detailDate];
NSInteger age = (NSInteger)duringTime / (365 * 24 * 60 * 60);
self.ageLabel.textColor = kGlobalBtn;
NSString *ageStr = [NSString stringWithFormat:@"%ld歲",(long)age + 1];