Ios NSComponents组件:fromDate:toDate:选项:度量小时还是天?
编辑:请标记为重复,找到答案 所以我想看看自从用户上次发布一个对象以来,这一天是否已经过去了。如果我使用Ios NSComponents组件:fromDate:toDate:选项:度量小时还是天?,ios,objective-c,nsdate,nscalendar,Ios,Objective C,Nsdate,Nscalendar,编辑:请标记为重复,找到答案 所以我想看看自从用户上次发布一个对象以来,这一天是否已经过去了。如果我使用 NSCalendarUnit units = NSDayCalendarUnit; NSDateComponents *components = [calendar components:units fromDate:lastPostDate toDate:now options:0]; 示例: 用户在晚上9点发布对象。然后我在第二天早上8点(11小时后)运行上述方法。它会告诉我0天已经过
NSCalendarUnit units = NSDayCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:lastPostDate toDate:now options:0];
示例:
用户在晚上9点发布对象。然后我在第二天早上8点(11小时后)运行上述方法。它会告诉我0天已经过去了,对吗?如果是这样的话,有没有一种方法可以告诉我我想找出什么,或者我应该手动去做呢?试试这个方法,我希望它能帮上忙
- (NSString*)getTimeDifferenceFromCurrentDate:(NSDate *) lastPostDate {
NSDate *currentDate = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSWeekCalendarUnit fromDate:messageDate toDate:currentDate options:0];
NSString *strTime = @"";
/*if (components.day > 0 || components.week >= 1) {
if (components.day >= 1||components.week >= 1||components.month >= 1){
NSDateFormatter *dfForCell = [[NSDateFormatter alloc] init];
[dfForCell setDateFormat:@"MMM dd, yyyy"];
NSLocale* my24HourLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
[dfForCell setLocale:my24HourLocale];
NSString *aStrFinalDate = [dfForCell stringFromDate:messageDate];
strTime = [NSString stringWithFormat:@"%@", aStrFinalDate];
};
}*/
if (components.year > 0) {
if (components.year > 1)
strTime = [NSString stringWithFormat:@"%d years ago", components.year];
else
strTime = [NSString stringWithFormat:@"%d year ago", components.year];
}
else if (components.month > 0) {
if (components.month > 1)
strTime = [NSString stringWithFormat:@"%d months ago", components.month];
else
strTime = [NSString stringWithFormat:@"%d month ago", components.month];
}
else if (components.week > 0) {
if (components.week > 1)
strTime = [NSString stringWithFormat:@"%d weeks ago", components.day];
else
strTime = [NSString stringWithFormat:@"%d day ago", components.day];
}
else if (components.day > 0) {
if (components.day > 1)
strTime = [NSString stringWithFormat:@"%d days ago", components.day];
else
strTime = [NSString stringWithFormat:@"%d day ago", components.day];
}
else if (components.hour > 0) {
if (components.hour > 1)
strTime = [NSString stringWithFormat:@"%dhours ago", components.hour];
else
strTime = [NSString stringWithFormat:@"%dhour ago", components.hour];
}
else if (components.minute > 0) {
if (components.minute > 1)
strTime = [NSString stringWithFormat:@"%dmin ago", components.minute];
else
strTime = [NSString stringWithFormat:@"%dmin ago", components.minute];
}
else if (components.second > 0) {
if (components.second > 1)
strTime = [NSString stringWithFormat:@"%dsec ago", components.second];
else
strTime = [NSString stringWithFormat:@"%dsec ago", components.second];
}
else {
strTime = @"1 sec ago";
}
return strTime;
}
我做到了,得到了我认为会得到的。我原封不动地发布了这个问题,因为我想让它保持开放状态,以防有一个选项/方法来做我想做的事情。另外,我的新手对这一点的信心让我怀疑自己。与其只是展示一堆代码,不如解释一下这是如何回答张贴的问题的。