获取正确的日期并设置通知 ios
Getting the correct date and set notification ios
我正在尝试为每个星期六和星期日设置重复通知,但由于某种原因日期即将到来wrong.This是我的代码
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear | NSCalendarUnitWeekOfMonth |NSCalendarUnitWeekOfMonth;
NSDateComponents *comps = [calendar components:unitFlags fromDate:[NSDate date]];
NSDateComponents *dt = [[NSDateComponents alloc]init];
comps.hour = hour;
comps.minute = [[goal.reminderTimeInfo valueForKey:@"minutes"] intValue];
[dt setMonth:[comps month]];
[dt setYear:[comps year]];
[dt setWeekOfMonth:[comps weekOfMonth]];
comps.weekday = 1; //Sunday
[dt setWeekday:[comps weekday]];
NSDate *sundayDate = [calendar dateFromComponents:dt];
它应该给出
2015-07-05 19:00:00 +0000
但它给予
2015-07-04 19:00:00 +0000
如果给本地通知一个特定的 date.For 示例,它 sunday.Will 它会在每个星期天触发?
您没有更改 dt 的小时和分钟,但更改了 comps。
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear | NSCalendarUnitWeekOfMonth |NSCalendarUnitWeekOfMonth;
NSDateComponents *comps = [calendar components:unitFlags fromDate:[NSDate date]];
NSDateComponents *dt = [[NSDateComponents alloc]init];
dt.hour = 19;
dt.minute = 00;
[dt setMonth:[comps month]];
[dt setYear:[comps year]];
[dt setWeekOfMonth:[comps weekOfMonth]];
[dt setWeekday:1];
NSDate *sundayDate = [calendar dateFromComponents:dt];
我正在尝试为每个星期六和星期日设置重复通知,但由于某种原因日期即将到来wrong.This是我的代码
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear | NSCalendarUnitWeekOfMonth |NSCalendarUnitWeekOfMonth;
NSDateComponents *comps = [calendar components:unitFlags fromDate:[NSDate date]];
NSDateComponents *dt = [[NSDateComponents alloc]init];
comps.hour = hour;
comps.minute = [[goal.reminderTimeInfo valueForKey:@"minutes"] intValue];
[dt setMonth:[comps month]];
[dt setYear:[comps year]];
[dt setWeekOfMonth:[comps weekOfMonth]];
comps.weekday = 1; //Sunday
[dt setWeekday:[comps weekday]];
NSDate *sundayDate = [calendar dateFromComponents:dt];
它应该给出
2015-07-05 19:00:00 +0000
但它给予
2015-07-04 19:00:00 +0000
如果给本地通知一个特定的 date.For 示例,它 sunday.Will 它会在每个星期天触发?
您没有更改 dt 的小时和分钟,但更改了 comps。
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear | NSCalendarUnitWeekOfMonth |NSCalendarUnitWeekOfMonth;
NSDateComponents *comps = [calendar components:unitFlags fromDate:[NSDate date]];
NSDateComponents *dt = [[NSDateComponents alloc]init];
dt.hour = 19;
dt.minute = 00;
[dt setMonth:[comps month]];
[dt setYear:[comps year]];
[dt setWeekOfMonth:[comps weekOfMonth]];
[dt setWeekday:1];
NSDate *sundayDate = [calendar dateFromComponents:dt];