所有NSDate
内容都需要考虑!我有float
这是24小时工作日的小数部分,我想将其转换为格式NSString
hh:mm AM/PM.
如果你知道你在做什么,我打赌这是一个单行...;)
答案 0 :(得分:1)
也许是这样的?
float day_fraction = 0.45; // This is your input fraction-of-day.
int day_seconds = 24 * 3600 * day_fraction;
NSDate *tod = [NSDate dateWithTimeIntervalSince1970:day_seconds];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"hh:mma"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSString *dateString = [dateFormat stringFromDate:tod];
NSLog(@"date: %@", dateString);
结果:
date: 10:48AM