ios - NSDateFormatter Returning Unexpected Time Values -


can explain me why following code returns inconsistent time values? i've been getting incorrect results when trying create nsdate object user specified date/time string , i've put following code below illustrate problem.

// create 2 strings containing current date , time nsdateformatter * dateformat = [[nsdateformatter alloc] init]; [dateformat setdateformat:@"yyyy-mm-dd"];   nsdateformatter * timeformat = [[nsdateformatter alloc] init];  [timeformat setdateformat:@"hh:mm:ss a"];  timeformat.amsymbol = @"am";  timeformat.pmsymbol = @"pm";  timeformat.timezone = [nstimezone timezonewithabbreviation:@"edt"];   nsdate * = [[nsdate alloc] init];  nsstring *thedate = [dateformat stringfromdate:now];  nsstring *thetime = [timeformat stringfromdate:now];   nslog(@"the current date/time (gtm): %@", now);  nslog(@"the current date/time (edt): %@ %@", thedate, thetime);   // combine date , time strings  nsmutablestring * thedatetime = [[nsmutablestring alloc] init];  thedatetime = [thedatetime stringbyappendingstring:thedate];  thedatetime = [thedatetime stringbyappendingstring:@" "];  thedatetime = [thedatetime stringbyappendingstring:thetime];   // define formatter parse combined date , time string  nsdateformatter * dateformatter=[[nsdateformatter alloc] init];  [dateformatter setdateformat:@"yyyy-mm-dd hh:mm:ss a"];  dateformatter.amsymbol = @"am";  dateformatter.pmsymbol = @"pm";  dateformatter.timezone = [nstimezone timezonewithabbreviation:@"edt"];   // create nsdate object using combined date , time string  nsdate * thedatetimeobject=[dateformatter datefromstring:thedatetime];  nsstring * thedatetimestring=[dateformatter stringfromdate:thedatetimeobject];   // print results  nslog(@"thedatetimeobject (gmt) = %@", thedatetimeobject);  nslog(@"thedatetimestring (edt) = %@", thedatetimestring); 

this code produces following output:

  current date/time (gmt): 2015-09-29 22:28:10 +0000   current date/time (edt): 2015-09-29 18:28:10 pm   thedatetimeobject (gmt) = 2015-09-29 16:28:10 +0000   thedatetimestring (edt) = 2015-09-29 12:28:10 pm 

clearly going wrong when combined date , time string gets parsed date formatter create nsdate object. seems not understand input time zone , returns time in gmt several hours off should (i.e. +4 hours). i've set time zone "edt", not sure else can fix problem, other hard code offset in input, i'd rather not do. appreciated.

you doing bad things using both 24-hour format (hh) instead of 12-hour format (hh) , using am/pm (a).

change both instances of hh in formats hh , should expected results.

you should set formatter's locale special locale en_us_posix avoid issues device's 24-hour time setting.

side note: use of nsmutablestring wrong. try this:

nsmutablestring * thedatetime = [[nsmutablestring alloc] init]; [thedatetime appendstring:thedate]; [thedatetime appendstring:@" "]; [thedatetime appendstring:thetime]; 

or use:

nsstring *thedatetime = [nsstring stringwithformat:@"%@ %@", thedate, thetime]; 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -