我正在写一个闹钟应用程序。在应用程序中,用户可以选择在当天设置闹钟的天数。但我不知道在一周内的特定日期设置UILocalNotification。
我如何知道重复间隔。我知道我必须设置
如何计算星期五会有多少天?
答案 0 :(得分:0)
您可以计算两个日期之间的天数,如下所示:
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit
fromDate:today
toDate:futureDate
options:0];
NSInteger daysBetweenDates = dateComponents.day;
答案 1 :(得分:0)
How to Get an NSDate for a Specific Day of Week and Time from an Existing NSDate - 本文将帮助您解决此问题。您可以从当前星期五获得NSDate:
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setLocale:[NSLocale currentLocale]];
NSDateComponents *nowComponents = [gregorian components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
[nowComponents setWeekday:6];
[nowComponents setWeek: [nowComponents week]]; // Current week
[nowComponents setHour:8]; // 8a.m.
[nowComponents setMinute:0];
[nowComponents setSecond:0];
NSDate *thisWeekFriday = [gregorian dateFromComponents:nowComponents];
然后,您可能想要检查thisWeekFriday
是否小于[NSDate date]
,如果是,请查看下周五。您可以使用以下日期将日期移动一周:
NSDateComponents *components = [[NSDateComponents alloc] init];
components.week = 1;
NSDate *nextWeekFriday = [gregorian dateByAddingComponents:components toDate:thisWeekFriday options:0];