ios - How to format the layout of an NSDate without converting to NSString -


i'm looking change way date object being put nsdictionary currently, date object being entered in following format:

2014-04-22 17:28:47 +0000 

however i'd entered as

22-04-2014 

i tried using nsdateformatter i've used it's put string

nsdateformatter *formatter = [[nsdateformatter alloc]init]; [formatter setdateformat:@"dd-mm-yyyy"]; nsstring *thedate = [formatter stringfromdate:date]; 

how achieve changing layout yyyy-mm-dd hh:m:ss ???? dd-mm-yyy maintain date object?

after having discussion op, been concluded following go long way in achieving desired result.

create new class looks this

//fooditem . h  nsstring *fooditemname; nsstring *fooditemdate; //you can add more stuff calorie intake etc  //have custom init method -(id)initnewfooditemname:(nsstring*)name withdate:(nsstring*)dateinput;   //then in  //fooditem.m  //make sure synthesise properties above. -(id)initnewfooditemname:(nsstring*)name withdate:(nsstring*)dateinput{     if (self = [super init]) {         self.fooditemname = name;         self.fooditemdate = dateinput;     }     return self; } 

this class data model use store data in database. if dont have 1 setup want have fun testing out searching algorithm, can create local temporary manual container live run time of program.

now whether want store in temporary database or in local database using apples sqlite database or using apples core data databases; you'll able awesome this:

nsmutablearray *temporarydb = [[nsmutablearray alloc] init]; [temporarydb addobject:[[fooditem alloc] initnewfooditemname:@"banana" withdate:@"2014-04-20"]]; [temporarydb addobject:[[fooditem alloc] initnewfooditemname:@"egg" withdate:@"2014-03-20"]]; [temporarydb addobject:[[fooditem alloc] initnewfooditemname:@"chocolate" withdate:@"2014-04-18"]]; 

then when user selects specific date because want display food items they've eaten on specific date, can extract date date picker, convert string using date formatter, , search through temporarydb array see if objects returned if date matches, so:

-(nsmutablearray*)searchforallfooditemsonacertaindate:(nsstring*)searchdate{     nsmutablearray *returnedresults = [[nsmutablearray alloc] init];     for(int = 0; < [temporarydb count]; i++){          fooditem *currentfooditem = [temporary objectatindex:i];          //if date found in temporary db store returnedresults array         if([currentfooditem.fooditemdate isequaltostring:searchdate]){              [returnedresults addobject:currentfooditem];            }     }     //in end have food items eaten on day or if none eaten, empty array returned size of 0      return returnedresults; } 

then somewhere else in program once user selects date, , clicks done button, can call search function so

 nsmutablearray *fooditemseatenonchosendate = [self searchforallfooditemsonacertaindate:datefromdatepicker];   if([fooditemseatenonchosendate count] > 0){     //these food items eaten on date user selected date picker } else{     //let user know didnt record food items on day } 

things remember

you've got make sure date string feed fooditemseatenonchosendate search function in same format saved dates in temporary db.

goodluck buddy.


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -