ios - NSDate - Adding Days doesn't change year value -


i have label want hold specific date. initial date should current date, , user should able step through time interval of day. after little research, found out, best set nsdatecomponents , add them current date.

my problem following: when step through time, seems fine, until reach end/beginning of year. instance, date hold in label 01.01.2014, when tap "previousdaybutton" become 31.12.2014. thought asking date , setting year manually, can't possibly think of solution.

so here code:

- (void)viewdidload {     [super viewdidload];     // additional setup after loading view.     [self setupinterface]; } - (void)setupinterface{     orderdate = [nsdate date];     [self updatedatelbl]; }  - (void)updatedatelbl{     nsdateformatter *dateformatter = [[nsdateformatter alloc]init];     [dateformatter setdateformat:@"dd.mm.yyyy"];     datelbl.text = [dateformatter stringfromdate:orderdate];     }  - (ibaction)nextday:(id)sender {     nsdatecomponents *comps = [[nsdatecomponents alloc]init];     comps.day = 1;      orderdate = [[nscalendar currentcalendar] datebyaddingcomponents:comps todate:orderdate options:0];     [self updatedatelbl]; }  - (ibaction)previousday:(id)sender {     nsdatecomponents *comps = [[nsdatecomponents alloc]init];     comps.day = -1;     orderdate = [[nscalendar currentcalendar] datebyaddingcomponents:comps todate:orderdate options:0];     [self updatedatelbl]; } 

i love :)

use xcode nsdateformatter. documentation:

it uses yyyy specify year component. common mistake use yyyy. yyyy specifies calendar year whereas yyyy specifies year (of “week of year”), used in iso year-week calendar. in cases, yyyy , yyyy yield same number, may different. typically should use calendar year.

2014 started on wednesday. tuesday, 31st dec. 2013, in same week - week 1 of 2014. that's yyyy displays. yyyy displays 2013.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -