Safari
Safari

Reputation: 11965

NSDate 24h format: delete first '0' from hours

When I want a date in 24 hour format, I see the hours such as 05:00. How can I delete the first 0 to get 5:00? I tried this:

NSDateFormatter *fhours = [[NSDateFormatter alloc] init];
[fhours setTimeStyle:NSDateFormatterShortStyle];
mystring = [fhours stringFromDate:newDate];
[fhours release];

Upvotes: 1

Views: 876

Answers (2)

simongking
simongking

Reputation: 740

You should get the right result with the following:

NSDateFormatter *fhours = [[NSDateFormatter alloc] init];
[fhours setDateFormat:@"H:mm"];
mystring = [fhours stringFromDate:newDate];
[fhours release];

Upvotes: 1

Blade
Blade

Reputation: 1437

Like this

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm"];
NSDate *now = [NSDate date];
NSString *theTime = [timeFormat stringFromDate:now];

At line

[timeFormat setDateFormat:@"HH:mm"];

You can erase one H like this

[timeFormat setDateFormat:@"H:mm"];

Upvotes: 7

Related Questions