iphone - how to change nsdate in nsdictionary -


i trying increment nsdate. have nsdictionary, trying increment nsdate content in it. nsdate 2011-07-11, want increment nsdate content in it.

got

{            conditiondatenew = "2011-07-21 13:31:46 +0000";     yesterday = "2011-07-20 13:31:46 +0000";     city = #;     condition = "isolated thunderstorms";     country = #;      "day_of_week" = sun;     high = 86;     icon = "chance_of_storm.gif";     low = 68;      state = #; } 

i want date 2011-07-22 in conditiondatenew in next dictionary loop. how can it?

create new date object, , replace old value in dictionary. dates immutable.

nsdate *newdate = [nsdate date]; [dictionary setobject:newdate forkey:@"conditiondatenew"]; 

[nsdate date] set "now."

the easiest way add day add 24 hours. works long dst never issue, , "increment day" mean "increment 24 hours." if work exclusively in utc, that's fine.

nsdate *newdate = [olddate datebyaddingtimeinterval:24*60*60]; 

if dst issue (and is), , "increment day" mean "increment gregorian calendar day" need use date components make sure add 23, 24 or 25 hours appropriate.

  nscalendar *calendar = [[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar];   nsdatecomponents *datecomponents = [[nsdatecomponents alloc] init];   [datecomponents setyear:2011];   [datecomponents setmonth:11];   [datecomponents setday:6];   [datecomponents settimezone:[nstimezone timezonewithname:@"america/new_york"]];   nsdate *date = [calendar datefromcomponents:datecomponents];    nsdatecomponents *addcomponents = [[nsdatecomponents alloc] init];   [addcomponents setday:1];   [addcomponents settimezone:[nstimezone localtimezone]];   nsdate *newdate = [calendar datebyaddingcomponents:addcomponents todate:date options:0];    nslog(@"olddate=%@", [date descriptionwithlocale:[nslocale currentlocale]]);   nslog(@"+24=%@", [[date datebyaddingtimeinterval:24*60*60] descriptionwithlocale:[nslocale currentlocale]]);   nslog(@"newdate=%@", [newdate descriptionwithlocale:[nslocale currentlocale]]);   [calendar release];   [datecomponents release];   [addcomponents release]; 

output:

olddate=sunday, november 6, 2011 12:00:00 eastern daylight time +24=sunday, november 6, 2011 11:00:00 pm eastern standard time newdate=monday, november 7, 2011 12:00:00 eastern standard time 

if don't care time, , have control on set to, 1 trick set time noon. way adding 24 hours fall on correct day. don't recommend because fails badly if ever forget , create date time of midnight. it's easier put of day-incrementing code in 1 place , fix dst problem 1 time make sure of date creation code right. recommend getting used date components.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -