开发者

convert time to 12hr format

开发者 https://www.devze.com 2023-01-30 01:16 出处:网络
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....You could use the 24 hour clock format instead of the

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....


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"];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号