Search code examples
iphoneobjective-cxcode4

how to show Time in 12 hours format in iphone


I am developing an app whenever a user add some details the record get stored in database as created date time in 24 hour format. But for display purpose i am showing the time in 12 hour format. i.e i have used "dd-MMM-yyyy hh:mm a" format to display the time and it show properly on my screen, but the problem is whenever user change the date/time format from 12 hour to 24 hour format in setting. My app start to show the time in 24 hour format. Can anyone plz help me that even though the user change the setting to 24 hour format in setting. Time format should not change from 12 to 24 hour.


Solution

  • In general, you should allow the user to choose how they want to display it.

    With your code, you should be able to do this:

    dateFormat = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSLocale *twelveHourLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
    dateFormat.locale = twelveHourLocale;
    CreatedDate = [dateFormat dateFromString:[NSString stringWithFormat:@"%@",[serviceAntInstance.arr_CreatedDate objectAtIndex:indexPath.row]]];
    [dateFormat setDateFormat:@"dd-MMM-yyyy hh:mm a"];
    createdDateConvert = [dateFormat stringFromDate:CreatedDate];