objective c - how to convert string value into date in iphone -
hi friend want convert string value date in project because fetching xml value web , in xml file weather condition , there value weather condition current date forecastcondition not come in date comes in string value friday saturday , sunday value come in forcast condition day string value come in day_of_week [day_of_week means shows friday,satarday,sunday ] want how can convert string value date please 1 me using please on controller class.m file
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { tweathercell *cell =(tweathercell *) [mytableview dequeuereusablecellwithidentifier:[nsstring stringwithformat:@"cell_%d", indexpath.row]]; if (cell == nil) { //cell = [[[tweathercell alloc] initwithstyle:uitableviewstylegrouped reuseidentifier:cellidentifier] autorelease]; cell = [[[tweathercell alloc] initwithframe:cgrectzero reuseidentifier:[nsstring stringwithformat:@"cell_%d", indexpath.row]] autorelease]; } //forecastcondition *cond=[forecastcond objectatindex:0]; forecastcondition *cond1=[forecastcond objectatindex:1]; forecastcondition *cond2=[forecastcond objectatindex:2]; forecastcondition *cond3=[forecastcond objectatindex:3]; nsdate *today = [nsdate date]; // valid date, let's today thursdaya nsdate *thursday = [today datewithtimeinterval:1 *one_day sincedate:cond1.dayofweek]; nsdate *friday = [today datewithtimeinterval:2 * one_day sincedate:cond2.dayofweek]; nsdate *saturday = [today datewithtimeinterval:3 * one_day sincedate:cond3.dayofweek]; if ([currentcond.icon isequaltostring:@"http://\n"]) { cell.weatherimage.image = [uiimage imagenamed:@"listicon-h.png"]; } else { nsdata *imagedata = [[nsdata alloc]initwithcontentsofurl:[nsurl urlwithstring:currentcond.icon]]; nslog(@"this image server:%@",imagedata); cell.weatherimage.image = [uiimage imagewithdata:imagedata]; [imagedata release]; } nsdateformatter *date_formatter=[[nsdateformatter alloc]init]; [date_formatter setdateformat:@"dd-mm-yyyy"]; switch (indexpath.row) { case 0: nslog(@"%d",indexpath.row); nsdata *imagedata = [[nsdata alloc]initwithcontentsofurl:[nsurl urlwithstring:currentcond.icon]]; nslog(@"this image server:%@",imagedata); cell.weatherimage.image = [uiimage imagewithdata:imagedata]; [imagedata release]; cell.reportdate.text = _forecastinfo.currentdatetime; cell.conditionname.text = currentcond.condition; cell.twotemp.text = [nsstring stringwithformat:@"temp:%@/%@",currentcond.tempf,currentcond.tempf]; cell.twodirection.text = currentcond.windcondition; cell.humidity.text = currentcond.humidity; break; case 1: nslog(@"%d",indexpath.row); cell.reportdate.text = cond1.dayofweek; cell.conditionname.text = cond1.condition; cell.twotemp.text = [nsstring stringwithformat:@"temp:%@/%@",cond1.low,cond1.high]; //nsdateformatter *date_formatter=[[nsdateformatter alloc]init]; // [date_formatter setdateformat:@"dd-mm-yyyy"]; //[cond.dayofweek datewithtimeintervalsincenow:(60*60*24)*1]; //nsdate *date1 = [date_formatter datefromstring:cond.dayofweek]; //nsdate *= nsdate *date1 ; //nsdate *date1 = [nsdate date]; //nsstring *strdate = [date_formatter stringfromdate:date1]; break; case 2: nslog(@"%d",indexpath.row); cell.reportdate.text = cond2.dayofweek; cell.conditionname.text = cond2.condition; cell.twotemp.text = [nsstring stringwithformat:@"temp:%@/%@",cond2.low,cond2.high]; break; case 3: nslog(@"%d",indexpath.row); cell.reportdate.text = cond3.dayofweek; cell.conditionname.text = cond3.condition; cell.twotemp.text = [nsstring stringwithformat:@"temp:%@/%@",cond3.low,cond3.high]; break; default: nslog(@"out of range ",indexpath.row); break; } return cell; }
i gather problem future dates coming in "thursday", "friday", ...?
basically, need convert text day-of-week number (simplest approach "if ladder", though if want locale-sensitive can use more complex scheme). figure out today's day-of-week number , subtract first number. if result negative, add 7. (if result zero, may mean refers today, or may mean refers week today -- if want latter, add 7 if result zero or negative.) then, compute date represented input day of week, add above computed result multiplied times one_day current date.
more detail
you know "if ladder" is, of course:
if ([day isequaltostring:@"sunday"]) { daynumber = 1; } else if ([day isequaltostring:@"monday"]) { daynumber = 2; } else if ([day isequaltostring:@"tuesday"]) { ....
to day of week current date use like:
nscalendar *gregorian = [[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar]; nsdatecomponents *components = [gregorian components:nsweekdaycalendarunit fromdate:today]; nsinteger todaynumber = [components weekday];
subtract todaynumber daynumber. if result negative, add 7 result. value tells how many days in future "day" is, , can calculate it's date.
Comments
Post a Comment