NSDate 24h format: delete first '0' from hours
-
14-11-2019 - |
Pregunta
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];
Solución
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"];
Otros consejos
You should get the right result with the following:
NSDateFormatter *fhours = [[NSDateFormatter alloc] init];
[fhours setDateFormat:@"H:mm"];
mystring = [fhours stringFromDate:newDate];
[fhours release];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow