datetime - iOS: Get localised hour from NSDate -


how localized hours nsdate respective 12 hr/24 hr time set user on device? consider date "3 jun 2015 3:00pm" if 12 hour format set should show 3pm (excluding minutes part) if 24 hour format set should show 15 (excluding minutes part)

i have tried following without success on locales. example working perfect de_de & fr_fr en_us returns 3 without am/pm.

nsdateformatter *dateformatter = [nsdateformatter new]; nsstring *localeformatstring = [nsdateformatter dateformatfromtemplate:@"hh" options:0 locale:dateformatter.locale]; dateformatter.dateformat = localeformatstring; dateformatter.timezone = [nstimezone defaulttimezone];  nsstring * hour = [dateformatter stringfromdate:date]; nslog(@"%@",hour); 

p.s. if time string (hours & minutes both) using following code, works respect locale 12/24 hour time format. want hours , not achieved 12/24 hour time format.

  nsdateformatter *timeformatter = [[nsdateformatter alloc] init];   timeformatter.timestyle = nsdateformattershortstyle;   timeformatter.dateformat = nsdateformatternostyle;   timeformatter.amsymbol = @"a";   timeformatter.pmsymbol = @"p";    timeformatter.timezone = timezone;   return [timeformatter stringfromdate:date]; 

you use nsdateformatter dd-mm-yyyy , use nstimezone applied formatter adjust local time zone.

nsdate *date = [[nsdate alloc] init]; nslog(@"%@", date);  nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdateformat:@"dd-mm-yyyy hh:mm a"]; nsstring *strdatestring = [dateformatter stringfromdate:date];  nslog(@"%@", strdatestring);  nstimezone *timezone = [nstimezone localtimezone]; [dateformatter settimezone:timezone]; nslog(@"adjusted timezone: %@", [dateformatter stringfromdate:date]); 

Comments