gpt4 book ai didi

ios - 如何通过增加时间来更改 NSDate 的日期?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:09:49 25 4
gpt4 key购买 nike

我有一个应用程序每小时触发一次 UILocalNotifications,它将在用户在日期选择器中选择的时间停止。我预先设置了 23 个本地通知,将小时分量加一个。问题是,当时间超过 12:00 AM 时,这一天仍然在同一天并且不会改变。当时间到达 12:00 AM 时,如何让 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];
}
}

最佳答案

改进:

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

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

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

原件(有时区问题):

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

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

关于ios - 如何通过增加时间来更改 NSDate 的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17033841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com