objective c - NSDate date method returns wrong result -


i know there lot of questions of type, did't find solution case; need current , correct nsdate object, not nsstring! code returns wrong time (+3 hours), because i'm ukraine.

nsdate *currentdate = [nsdate date]; 

how current nsdate object nsdateformatter? know how nsstring, don't need it.

edit: i'm using code compare 2 nsdates using nscalendar object, here code:

nscalendar *calendar = [nscalendar currentcalendar]; nsuinteger unitflags = nsdaycalendarunit | nshourcalendarunit | nsminutecalendarunit | nssecondcalendarunit; nsdatecomponents *components = [calendar components:unitflags fromdate:nowdate todate:enddate options:0]; 

and components.hour shows me +3 hours difference

nsdates stored in utc, dates itselfs dont know timezones or weeks, month, years. point in time.

to see correct time position on earth surface, need take nscalendar, represents time model in account. use directly , mess around dates, or create nsdateformatter leave dates untouched adjust appearence needs.

nsdateformatter *formatter = [[nsdateformatter alloc] init]; [formatter setdatestyle:nsdateformatterfullstyle]; [formatter settimestyle:nsdateformatterfullstyle];  nslog(@"%@", [formatter stringfromdate: date1]); 

results in

thursday, july 12, 2012, 4:36:07 pm central european summer time  

in response comment:

try code test

nsdate *now = [nsdate date]; nsdate *startoftoday = nil; nsdate *startofthisweek = nil; nsdate *startofthismonth = nil; nsdate *startofthisyear = nil; [[nscalendar currentcalendar] rangeofunit:nsdaycalendarunit startdate:&startoftoday interval:null fordate:now]; [[nscalendar currentcalendar] rangeofunit:nsweekcalendarunit startdate:&startofthisweek interval:null fordate:now]; [[nscalendar currentcalendar] rangeofunit:nsmonthcalendarunit startdate:&startofthismonth interval:null fordate:now]; [[nscalendar currentcalendar] rangeofunit:nsyearcalendarunit startdate:&startofthisyear interval:null fordate:now];  nsdateformatter *formatter = [[nsdateformatter alloc] init]; [formatter setdatestyle:nsdateformatterfullstyle]; [formatter settimestyle:nsdateformatterfullstyle];  nslog(@"%@", now); nslog(@"%@", [formatter stringfromdate:now]);  nslog(@"%@", startoftoday); nslog(@"%@", [formatter stringfromdate:startoftoday]);  nslog(@"%@", startofthisweek); nslog(@"%@", [formatter stringfromdate:startofthisweek]);  nslog(@"%@", startofthismonth); nslog(@"%@", [formatter stringfromdate:startofthismonth]);  nslog(@"%@", startofthisyear); nslog(@"%@", [formatter stringfromdate:startofthisyear]); 

you realize, start of day, week, month , year adjusted local time, first of each nslog-pair give date in utc , second in local time zone.


on chat posted code

nsdateformatter *dateformat = [[nsdateformatter alloc] init];  [dateformat setdateformat:@"yyyy-mm-dd hh:mm"];  [dateformat settimezone:[nstimezone timezoneforsecondsfromgmt:0]];  nsdate *date = [dateformat datefromstring:datestring];  [dateformat release];  

so problem is, datestring not gmt, eet (eastern european time)

try

[dateformat settimezone:[nstimezone timezonewithabbreviation:@"eet"]];  

but far elegant solution datestring offset utc, similar 2012-07-12 12:23:00 +0300if how possible.
in such case parse with
[dateformat setdateformat:@"yyyy-mm-dd hh:mm:ss z"]; , don't need further time zone handling, formatter knows offset via z-specifier.
note, if don't set timezone, device's current should used. if user in same timezone time date string, should work, too. if user leaves zone, or inted have working world wide, should use 1 of solutions gave you. second (specifying timezone with-in datestring) preferred one.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -