iphone - How to get an NSDate from a specific time zone and perform calculations based on that date -
i have been trying wrap head around nsdate
, nscalendar
, nsdatecomponents
, nstimezone
, , nsdateformatter
.
nsdate
, nstimezone
, nsdateformatter
pretty straightforward. other classes not.
i want current eastern time (the actual date/time in est @ moment code run).
then want advance date 1 month, taking account daylight savings may (or may not be) in effect.
from understand, adding 84600 improper way this, wrong.
can please post example of how this?
it's pretty easy:
nsdate *now = [nsdate date]; nsdatecomponents *onemonth = [[[nsdatecomponents alloc] init] autorelease]; [onemonth setmonth:1]; nscalendar *calendar = [nscalendar currentcalendar]; nsdate *onemonthfromnow = [calendar datebyaddingcomponents:onemonth todate:now options:0]; nsdateformatter *df = [[[nsdateformatter alloc] init] autorelease]; [df settimezone:[nstimezone timezonewithname:@"america/new_york"]]; [df setdatestyle:nsdateformattermediumstyle]; [df settimestyle:nsdateformattermediumstyle]; nslog(@"now in new york: %@", [df stringfromdate:now]); nslog(@"one month in new york: %@", [df stringfromdate:onemonthfromnow]);
edit: said, question phrased little confusingly. nsdate, don't calculations in specific time zone. don't "current eastern time". current point in time (regardless of time zone) , calculations on it. actual conversion specific time zone happens @ output (when display time user, in time zone).
Comments
Post a Comment