0

如何获得每个月的第 5 个日期。到目前为止,我知道如何获取当前日期。我只想在每个月的 5 日发出本地通知。

这是我的代码:

 UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease];
 if (!localNotification){
         return; 
 }
 NSDate *date = [NSDate date];   
 NSDate *dateToFire = ?;  // please give me logic to find out 5th of evry month


 [localNotification setFireDate:dateToFire];
 [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];


 NSArray *array = [NSArray arrayWithObjects:@"Value 1", @"Value 2", nil];
 NSDictionary *data = [NSDictionary dictionaryWithObject:array forKey:@"payload"];
 [localNotification setUserInfo:data];



 [localNotification setAlertBody:@"Incoming Local Notification" ];
 [localNotification setAlertAction:@"Open App"];
 [localNotification setHasAction:YES];      

 [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
4

1 回答 1

1

如果我理解你的问题,那么你可以这样做:

NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger day = [dateComponents day];
if(day==5){
     //your logic starts or make some flag here.
}
于 2012-12-05T10:46:19.940 回答