我在写报警应用程序。在应用程序中,用户可以随时选择日期来设置当天的闹钟。但我不知道在一周内的特定日期设置UILocalNotification。特定日期的个性化信息
我如何知道重复间隔。我知道我必须设置
如何计算星期五会有多少天?
我在写报警应用程序。在应用程序中,用户可以随时选择日期来设置当天的闹钟。但我不知道在一周内的特定日期设置UILocalNotification。特定日期的个性化信息
我如何知道重复间隔。我知道我必须设置
如何计算星期五会有多少天?
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];
可以计算天数两个日期之间是这样的:
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit
fromDate:today
toDate:futureDate
options:0];
NSInteger daysBetweenDates = dateComponents.day;
今天是星期二。我将通知设置为星期一,并且工作正常。为什么?如何仅在下个星期一设置通知? –
@BogdanKorda:我已经更新了我的答案。 –