objective c - Getting timezone offset from a date string -
i have following date string: 2015-04-18t08:35:00.000+03:00 , offset gmt in seconds, i.e. 10800.
i can split date string '+' character, have string xy:ab , calculate offset formula xy*3600 + a*60 + b.
is there better way achieve need, example using nsdate , nsdateformatter?
i've ended using code:
nsstring *datestringwithtz = @"2015-04-18t08:35:00.000+03:00"; // create date object using timezone input string. nsdateformatter *dateformatterwithtz = [[nsdateformatter alloc] init]; dateformatterwithtz.dateformat = @"yyyy-mm-dd't'hh:mm:ss.sssz"; nsdate *datewithtz = [dateformatterwithtz datefromstring:datestringwithtz]; // cut off timezone , create date object using gmt timezone. nsstring *datestringwithouttz = [datestringwithtz substringwithrange:nsmakerange(0, 23)]; nsdateformatter *dateformatterwithouttz = [[nsdateformatter alloc] init]; dateformatterwithouttz.dateformat = @"yyyy-mm-dd't'hh:mm:ss.sss"; dateformatterwithouttz.timezone = [nstimezone timezonewithabbreviation:@"gmt"]; nsdate *datewithouttz = [dateformatterwithouttz datefromstring:datestringwithouttz]; // calculate difference. nsinteger difference = [datewithouttz timeintervalsincereferencedate] - [datewithtz timeintervalsincereferencedate]; return [nsnumber numberwithinteger:difference];
Comments
Post a Comment