您好,我有一个这样的日期字符串。{{1}}。我想做的是检查它是否是
今天或本周或本月。我可以比较今天和月份。但是我如何检查这个日期是否属于"本周"
请帮帮我。 感谢
答案 0 :(得分:3)
鉴于两个日期,
NSDate *now = [NSDate date];
NSDate *then = // some other date
了解他们是否在同一周内与:
BOOL sameWeek = [[NSCalendar currentCalendar] isDate:then equalToDate:now
toUnitGranularity:NSCalendarUnitWeekOfYear];
该行询问他们是否相等"一周的粒度,意味着他们是平等的#34;如果他们在同一周内。
答案 1 :(得分:0)
尝试以下代码,
- (void)thisWeek:(NSDate *)date
{
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *todaysComponents = [gregorian components:NSCalendarUnitWeekOfYear fromDate:[NSDate date]];
NSUInteger todaysWeek = [todaysComponents weekOfYear];
NSDateComponents *otherComponents = [gregorian components:NSCalendarUnitWeekOfYear fromDate:date];
NSUInteger datesWeek = [otherComponents weekOfYear];
//NSLog(@"Date %@",date);
if(todaysWeek==datesWeek){
NSLog(@"Date is in this week");
}else if(todaysWeek+1==datesWeek){
NSLog(@"Date is in next week");
}
}