objective-c how to get days of Previous week? -
how can array of days of previous week? example - today wed/04/2013, forth day of week if first day of week sunday. need array ' sunday/25/2013', mon/26/2013,..... sat/31/2013? doing didn't work required..
nscalendar *mycalendar = [[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar]; nsdatecomponents *currentcomps = [mycalendar components:( nsyearcalendarunit | nsmonthcalendarunit | nsweekofyearcalendarunit | nsweekdaycalendarunit | nshourcalendarunit | nsminutecalendarunit) fromdate:weekdate]; int ff = currentcomps.weekofyear; nslog(@"1 %d", ff); [currentcomps setday:2]; // 1: sunday [currentcomps setweek: [currentcomps week] - 1]; nslog(@"currentcomps setweek:>>>>>> %@", currentcomps); nsdate *firstdayoftheweek = [mycalendar datefromcomponents:currentcomps]; nslog(@"firstdayoftheweek>>>>>> %@", firstdayoftheweek); nsstring *firststr = [mydateformatter stringfromdate:firstdayoftheweek]; lbl_day1.text = firststr;
// start date, e.g. now: nsdate *now = [nsdate date]; nscalendar *cal = [nscalendar currentcalendar]; // compute beginning of current week: nsdate *date; [cal rangeofunit:nsweekcalendarunit startdate:&date interval:null fordate:now]; // go 1 week start of previous week: nsdatecomponents *comp1 = [[nsdatecomponents alloc] init]; [comp1 setweek:-1]; date = [cal datebyaddingcomponents:comp1 todate:date options:0]; // output format (adjust needs): nsdateformatter *fmt = [[nsdateformatter alloc] init]; [fmt setdateformat:@"eeee dd/mm/yyyy"]; // repeatedly add 1 day: nsdatecomponents *comp2 = [[nsdatecomponents alloc] init]; [comp2 setday:1]; (int = 1; <= 7; i++) { nsstring *text = [fmt stringfromdate:date]; nslog(@"%@", text); date = [cal datebyaddingcomponents:comp2 todate:date options:0]; } output:
sunday 25/08/2013 monday 26/08/2013 tuesday 27/08/2013 wednesday 28/08/2013 thursday 29/08/2013 friday 30/08/2013 saturday 31/08/2013
added (in reply comment):
nsdatecomponents *comp2 = [[nsdatecomponents alloc] init]; [comp2 setday:1]; // first day: lbl_day1.text = [fmt stringfromdate:date]; // second day: date = [cal datebyaddingcomponents:comp2 todate:date options:0]; lbl_day2.text = [fmt stringfromdate:date]; // third day: date = [cal datebyaddingcomponents:comp2 todate:date options:0]; lbl_day3.text = [fmt stringfromdate:date]; // , on ...
Comments
Post a Comment