nsdate - objective-c: Conversion between TimeZones -
i'm having problems converting time string different timezone.
this string example 1 retrieve server: @"5/14/2013 4:19am"
i know timezone america/new_york , want convert local timezone: europe/rome.
here code wrote test it:
-(void) converttimezone { nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; dateformatter.timezone=[nstimezone timezonewithabbreviation:@"gmt"]; dateformatter.dateformat=@"mm/dd/yyyy hh:mma"; nsstring *sourcedatestring = @"5/14/2013 4:19am"; nstimezone *sourcetimezone = [[nstimezone alloc] initwithname:@"america/new_york"]; nstimezone *destinationtimezone = [nstimezone localtimezone]; nsinteger sourcegmtoffset = sourcetimezone.secondsfromgmt; nsinteger destinationgmtoffset = destinationtimezone.secondsfromgmt; nsinteger interval = destinationgmtoffset-sourcegmtoffset; nslog(@"dateformatter timezone: %@", dateformatter.timezone); nslog(@"source timezone: %@", sourcetimezone); nslog(@"destination timezone: %@", destinationtimezone); nslog(@"sourceoffset: %u destinationoffset: %u interval: %u", sourcegmtoffset, destinationgmtoffset, interval); nslog(@"----------------------------------------------------------------------------------------------------"); nslog(@"sourcedatestring : %@", sourcedatestring); //convert string date nsdate *dateobject = [dateformatter datefromstring:sourcedatestring]; nslog(@"from string date: %@", dateobject); //now date string nslog(@"from date string: %@", [dateformatter stringfromdate:dateobject]); //convert sourcetimezone destinationtimezone nsdate *newdate = [[nsdate alloc] initwithtimeinterval: interval sincedate:dateobject]; nslog(@"destinationdatestring: %@", [dateformatter stringfromdate: newdate]); }
output:
dateformatter timezone: gmt (gmt) offset 0
source timezone: america/new_york (gmt-04:00) offset -14400 (daylight)
destination timezone: local time zone (europe/rome (cest) offset 7200 (daylight))
sourceoffset: 4294952896 destinationoffset: 7200 interval: 21600
sourcedatestring : 5/14/2013 4:19am
from string date: 2013-05-14 00:19:00 +0000
from date string: 05/14/2013 00:19am
destinationdatestring: 05/14/2013 06:19am
i'm getting mad!!!!
if original date "5/14/2013 4:19am" why date formatter changes "05/14/2013 00:19am" ????????? if had saved correctly "05/14/2013 04:19am +0000" conversion perfect (6 hours between 2 timezones)!!!
any hint?
thanks
nicola
edit
by setting these parameters suggested lithu-t-v:
dateformatter.timezone=[nstimezone timezonewithabbreviation:@"gmt"]; nsstring *sourcedatestring = @"5/14/2013 4:19am -0400";
it seems work doesn't.
i "it seems" because:
sourcedatestring: 5/14/2013 04:19am -0400
from string date: 2013-05-13 04:19:00 +0000 (ok)
from date string: 05/14/2013 04:19 (ok)
destinationdatestring: 05/14/2013 10:19 (correct)
is correct if try late pm hour:
sourcedatestring: 5/14/2013 10:19pm -0400
from string date: 2013-05-14 16:19:00 +0000 (wrong)
from date string: 05/14/2013 16:19
destinationdatestring: 05/14/2013 22:19 (wrong!)
the correct result should be: 05/15/2013 04:19am
the +0000 component defines date time zone , provide actual time .a nsdate object include part also.since part missing time difference comes,with respect local time zone
here ceated timezone not set datefromatter.set timezone dateformatter , try it
append +0000 string , remaining.it work
Comments
Post a Comment