iphone - UIDatePicker & NSDateFormatter returning incorrect date -
i have allready tryed solutions few posts, no luck. problem i'm facing whit time zones. both on device , simulator uidatepicker , nsdateformatter returning incorrect dates, adding or substracting difference gmt 0, selected date (according current time zone set on device or mac).
i have allready tried setting locale , time zone nothing worked. idea wellcomed! thanks.
(haven't posted code sample because both initialized without setting properties right now.)
update 1:
here code snippet:
datepicker = [[uidatepicker alloc] initwithframe:cgrectmake(0.0, frame.size.height - 216.0, frame.size.width, 216.0)]; [self addsubview:datepicker]; .... nsdateformatter *formatter = [[nsdateformatter alloc] init]; [formatter setdateformat:@"eee, mmm d, y h:mma"]; nsstring *datestring = [formatter stringfromdate:datepicker.date]; [formatter release];
found solution, time zone difference should added in following way.
- (nsdate *)datetogmt:(nsdate *)sourcedate { nstimezone* destinationtimezone = [nstimezone systemtimezone]; nsinteger destinationgmtoffset = [destinationtimezone secondsfromgmtfordate:sourcedate]; nsdate* destinationdate = [[[nsdate alloc] initwithtimeinterval:destinationgmtoffset sincedate:sourcedate] autorelease]; return destinationdate; }
Comments
Post a Comment