convert time to 12hr format

Please tell me how to convert time to 12 hr format.. I have a string which i have converted to date but it is in 24 hr format....

Answers


You could use the 24 hour clock format instead of the 12 hour format. So use a NSDateFormatter with H in the format string. Using one of the predefined formats will end up using your systems localization which probably is the 12 hour format.

NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"HH:mm"];

another solution is presented here using regex: 24hr to 12hr time convert, insert character to string


considering you have an hour int named time...

if (time>12) time=time-12;


When you say you've converted it to a date, what do you mean? Do you mean you have it in an NSDate object? If so, the date is not in 24hr date format. NSDate does not concern itself with how the date will look on-screen.

The formatting comes from your localisation settings. Which is how it should be. I don't want times in 12hr format any more than you want them in 24hr.

But to directly answer the question, you may have to write your own formatting code if you want to override the defaults. I had to do this to work around bugs in NSDateFormatter when I received dates from a third party data source.


To change the date format from 12 to 24 hours or from 24 to 12 simply change hh:mm to HH:mm format where h is for 12 hours and H is for 24 hours. Remember to change your location.

NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"HH:mm"];

Need Your Help

Resize font-size on screen resolutions

html css text

how can i resize font-size on screen resolution becouse i want the text to not be more than 1 line when screen resolution its to small. For example: http://prntscr.com/2qa9ze

How do I create a search form for my posts with Sinatra and Sequel?

ruby forms search sinatra sequel

I am new to Ruby, and am using Sinatra and Sequel. I'm trying to implement a form to search through the title of my posts.