Question

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];
Was it helpful?

Solution

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

OTHER TIPS

You should get the right result with the following:

NSDateFormatter *fhours = [[NSDateFormatter alloc] init];
[fhours setDateFormat:@"H:mm"];
mystring = [fhours stringFromDate:newDate];
[fhours release];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top