Reputation: 1097
I'm using this app here it works fine with me but I want to insert the fireDate of local notification manually instead of using the datePicker.
notif.fireDate = [datePicker date];
How can I insert date (day, month, year, hours and minutes) manually?
I tried the following
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:10];
[dateComps setMonth:3];
[dateComps setYear:2013];
[dateComps setHour:4];
[dateComps setMinute:45];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
int minutesBefore = 2;
notif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];
but it not works with me.
thanks in advance.
Upvotes: 0
Views: 3300
Reputation: 31073
Using NSDateFormatter you can convert directly from a NSString
to a NSDate
. Here's an example:
NSDateFormatter* myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:@"MM/dd/yyyy"];
NSDate* myDate = [myFormatter dateFromString:@"8/26/2012"];
NSLog(@"%@", myDate);
EDIT
If you want time too
NSString *str =@"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM/dd/yyyy hh:mm a"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[formatter setTimeZone:gmt];
NSDate *date = [formatter dateFromString:str];
NSLog(@"%@",date);
Follow this link for more info SO Answer
And for localNotification just implement this too after fire date
UIApplication *app=[UIApplication sharedApplication];
[app scheduleLocalNotification:localNotification];
Upvotes: 2
Reputation: 5782
How about using addTimeInterval
?
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];
//minutesBefore will be type of int and itemDate would be of NSDate type.
Update 1:
After searching little I found out addTimeInterval
is deprecated in iOS 4 and further. Instead you should use dateByAddingTimeInterval
.
localNotif.fireDate = [itemDate dateByAddingTimeInterval:-(minutesBefore*60)];
Providing negative value to dateByAddingTimeInterval
will fire notification minutes earlier to the itemDate
.
Upvotes: 0