runmad
runmad

Reputation: 14886

Convert "minutes since midnight" to time (convert to NSDate?)

I am getting some open/closed times as integers since midnight. For example:

"1140" = 19:00 (or 7 pm if you will) or; "570" = 9:30 (or 9.30 am)

I can easily get the time in European format, just by doing doing a simple calculation and formatting:

[NSString stringWithFormat:@"%d:00", (T / 60)]

However, this doesn't work for half hours, such as "570 minutes since midnight". Also, I run into problems if I want to include a bit of localization, because some people may prefer AM/PM times.

Does NSDate include a method for easily implementing times using the above? Or can anyone tell me how I would at least convert a time like "570 minutes since midnight" to 9:30 properly. Right now it obviously won't write the :30 part.

Thank you

Upvotes: 0

Views: 1322

Answers (2)

Paulo Santos
Paulo Santos

Reputation: 11587

You have your own answer:

[NSString stringWithFormat:@"%d:%d", (T / 60), (T % 60)] 

Upvotes: 1

Claus Broch
Claus Broch

Reputation: 9212

The simple answer could be to just compute the hours and minutes separately:

hours = T / 60;
mins = T % 60;

When it comes to displaying of time and converting this to a NSString you should let the NSDateFormatter do all the dirty work for you.

Claus

Upvotes: 2

Related Questions