datepicker - How to convert a string Date & Time to NSDate in iOS? -
this question has answer here:
- how convert string date format in ios? 2 answers
i facing issue last 2 days. requirement this.
i have start date string. : start_date == 15-07-15
and end date string. : end_date == 16-07-15
and time like: start time == 16:28, end time == 18:28
i combine 2 string , convert nsdate
.
nsstring *strt_date = [nsstring stringwithformat:@"%@ %@",start_date, start_time]; nsdateformatter *dateformat = [[nsdateformatter alloc] init]; [dateformat settimezone:[nstimezone timezonewithname:@"gmt"]]; [dateformat setdateformat:@"dd-mm-yy hh:mm"]; nsdate *date = [dateformat datefromstring:strt_date];
output :
date == 2015-07-15 11:01:00 +0000
then fire notification :
[self schedulenotificationfordate:date]; - (void)schedulenotificationfordate:(nsdate *)date { // here cancel scheduled notifications [[uiapplication sharedapplication] cancelalllocalnotifications]; nslog(@"date == %@",date); uilocalnotification *localnotif = [[uilocalnotification alloc] init]; if (localnotif == nil) return; localnotif.firedate = date;// sets when notification called. // notification details localnotif.timezone = [nstimezone defaulttimezone]; localnotif.alertbody = @"time read course";// shows alert box message. // set action button localnotif.alertaction = @"view"; localnotif.soundname = uilocalnotificationdefaultsoundname; localnotif.applicationiconbadgenumber = -1; // schedule notification [[uiapplication sharedapplication] schedulelocalnotification:localnotif]; }
note: device in 24 hr format , date format 24 hr format.
my question notification not firing. please me.
first create single string date & time.
nsstring *strt_date = [nsstring stringwithformat:@"%@ %@",start_date,start_time]; nsstring *endd_date = [nsstring stringwithformat:@"%@ %@",end_date,end_time];
than pass string following function
-(nsdate*)datefromstring:(nsstring*)strdate { nsdateformatter *dateformatter = [[nsdateformatter alloc]init]; dateformatter.dateformat = @"dd-mm-yy hh:mm"; return [dateformatter datefromstring:strdate]; }
then set firedate
Comments
Post a Comment