有没有办法检查 NSDate 是本周还是下周?
我知道今天是:
[NSDate date]
然后我该怎么办?
使用 NSDateComponents,如下所示:
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *today = [NSDate date];
NSDateComponents *todaysComponents =
[gregorian components:NSWeekCalendarUnit fromDate:date];
NSUInteger todaysWeek = [todaysComponents week];
NSDate *anotherDate = [NSDate date];
NSDateComponents *otherComponents =
[gregorian components:NSWeekCalendarUnit fromDate:anotherDate];
NSUInteger anotherWeek = [otherComponents week];
if(todaysWeek==anotherWeek){
NSLog(@"another date is this week");
}else if(todaysWeek+1==anotherWeek){
NSLog(@"another date is next week")
}
您还可以使用其他组件(例如月份或年份)来完全确定。
NOTE: 不要使用时间间隔。通过使用 NSDateComponents,您可以忽略小时、分钟和秒。我想你想要那个。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)