如何通过增加小时来改变NSDate的一天?

时间:2013-06-10 22:27:35

标签: ios cocoa nsdate uilocalnotification

我有一个应用程序每小时触发UILocalNotifications,这将在用户在日期选择器中选择时停止。我通过将小时组件加1来预先设置23个本地通知。问题是,当小时从凌晨12:00开始,那天停留在同一天并且不会改变。当小时到达凌晨12:00时,我如何更改NSDate?谢谢您的帮助。

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

NSDate *pickerDate = [self.datePicker date];

NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                               fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                               fromDate:pickerDate]; 

NSDateComponents *dateComps = [[NSDateComponents alloc] init];

[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:0];

//date chosen from the picker with seconds = 0 i.e., final alarm
NSDate *itemDate = [calendar dateFromComponents:dateComps];


//setting the final alarm

UILocalNotification *finalAlarm = [[UILocalNotification alloc] init];
if (finalAlarm) {
    finalAlarm.fireDate = itemDate;
    finalAlarm.timeZone = [NSTimeZone defaultTimeZone];
    finalAlarm.repeatInterval = 0;
    finalAlarm.soundName = UILocalNotificationDefaultSoundName;
    finalAlarm.alertBody = @"Test message...";
    [[UIApplication sharedApplication] scheduleLocalNotification:finalAlarm];
}  

//Formatting original date to isolate the current hour
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH"];

//Formatting original date to isolate the current minute
NSDateFormatter *minuteFormatter = [[NSDateFormatter alloc]init];
[minuteFormatter setDateFormat:@"mm"]; 

//creating a NSDateFormatter for readability
NSDateFormatter *yearMonthDay = [[NSDateFormatter alloc]init];
[yearMonthDay setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];


//isolate current date/hour/minute
NSDate *currentDate = [NSDate date];
NSString *currentHour = [dateFormatter stringFromDate:currentDate];
NSString *currentMinute = [minuteFormatter stringFromDate:currentDate];


NSString *currentForamttedDate = [yearMonthDay stringFromDate:currentDate];
NSLog(@"Current Date: %@",currentForamttedDate); 

//current hour int
int currentHourInt = [currentHour intValue];

//current minute int
int currentMinuteInt = [currentMinute intValue];    


//current hour plus 1
int firstAlarmHour = currentHourInt + 1;

//creating the first alarm
NSDateComponents *firstAlarm = [[NSDateComponents alloc]init];

[firstAlarm setDay:[dateComponents day]];
[firstAlarm setMonth:[dateComponents month]];
[firstAlarm setYear:[dateComponents year]];
[firstAlarm setHour:firstAlarmHour];
[firstAlarm setMinute:currentMinuteInt];
[firstAlarm setSecond:0];

//creating a date from the components
NSDate *firstAlarmDate = [calendar dateFromComponents:firstAlarm];

//setting the first alarm
[self scheduleNotificationForDate: firstAlarmDate finalAlarm:itemDate];

-(void) scheduleNotificationForDate: (NSDate*)date finalAlarm: (NSDate *)finalAlarmDate {


NSComparisonResult result = [finalAlarmDate compare:date];

    if (alarm) {
        alarm.fireDate = date;
        alarm.timeZone = [NSTimeZone defaultTimeZone];
        alarm.repeatInterval = 0;
        alarm.soundName = UILocalNotificationDefaultSoundName;
        alarm.alertBody = @"Test message...";
        [[UIApplication sharedApplication] scheduleLocalNotification:alarm];
    }
}

1 个答案:

答案 0 :(得分:3)

改进:

创建一个小时的日期组件,然后获取日历以将该组件添加到先前计算的(初始)开火日期:

NSDateComponents *hourComponent = [[NSDateComponents alloc] init];
hourComponent.hour = 1;

dateToBeIncremented = [theCalendar dateByAddingComponents:hourComponent toDate:dateToBeIncremented options:0];

原创(有时区问题):

如何使用日期组件来组织第一个通知的日期,然后使用NSDate dateByAddingTimeInterval:通过添加几天的秒来计算后续日期。

更好的选择可能是创建一个小时的辩论组件。获得第一个开火日期后,使用小时日期组件生成下一个开火日期,然后使用该日期生成下一个日期。与上述方法相比,这应该处理更多与时区相关的问题。