我有一个0.27392的双倍,我知道它是6:34:27 AM。这在Excel中很简单,但我无法让NSDateFormatter发挥其魔力。
答案 0 :(得分:2)
double time = 0.27392;
double timeInHours = time*24; // 6.57408
int hours = (int) timeInHours; // 6
int minutes = (timeInHours - floor(timeInHours)) * 60; // 0.57408*60=34.4448 → 34
......等等。
答案 1 :(得分:1)
哦好的,很棒的抓住zoul。
在那种情况下我会这样做:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
double time = 0.27392;
double timeInSeconds = time*24*60*60; // 0.27392 * 24 = 6.57408 hours *60 for minutes * 60 for seconds
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:timeInSeconds]; //Creates a date: 1 January 2001 6:34:27 AM, GMT
NSLog(@"Time: %@", [dateFormatter stringFromDate:date]);
[dateFormatter release];