2012-09-05 86 views
-2

我有一個字符串值20.30(小時)我需要它轉換08:30:00。 任何人都可以知道這一點,請幫助我。將浮點數轉換爲時間

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    dateFormat.dateFormat = @"hh:mm:ss"; 
    [dateFormat setTimeZone:[NSTimeZone systemTimeZone]]; 

    double time = 20.30; 
    double timeInSeconds = time*24*60*60; // 0.27392 * 24 = 6.57408 hours *60 for minutes * 60 for seconds 

    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:timeInSeconds]; //Creates a date: 1 January 2001 6:34:27 AM, GMT 

    NSLog(@"Time: %@", [dateFormat stringFromDate:date]); 
    [dateFormat release]; 

上面的代碼不適用於我。

+2

我認爲你需要做的是創建一個格式@「hh.mm」的日期格式化程序來匹配你的字符串。然後使用這個格式化程序從字符串中創建一個NSDate對象。然後您需要另一個格式化器,輸出格式@「hh:mm:ss」,然後使用它將您的NSDate轉換爲字符串!希望這有助於, –

+2

也只是一個說明,不太清楚你想用timeInSeconds實現什麼?由於20:30不是20.30 * 24 * 3600秒,除非您在打電話時非常幸運。 –

+0

同意@ GeorgeGreen的第一條評論..查看我的回答 –

回答

2

這個替換您的代碼:

//The Format which you want as output 
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
dateFormat.dateFormat = @"hh:mm:ss"; 
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]]; 

//The Format in which your dateTime currently is 
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init]; 
dateFormat1.dateFormat = @"HH.mm"; 
[dateFormat1 setTimeZone:[NSTimeZone systemTimeZone]]; 

double time = 20.30; 

NSString *timeStr = [NSString stringWithFormat:@"%.2f",time]; 
NSDate *dates = [dateFormat1 dateFromString:timeStr]; 
NSLog(@"Time: %@", [dateFormat stringFromDate:dates]); 
+0

這不會正確地轉換時間。 20:50:00 20.5小時出現,這是錯誤的。它應該出現爲20:30:00 –

+0

@ Izac,我感謝你的貢獻,但如果你仔細看看這個問題第一行說**「字符串值20.30(小時)我需要它轉換爲08: 30:00「**間接表示20.5 = 8:50:00而不是8:30:00。另外,您已將」hh「更改爲」HH「,表明它是錯誤的。我想讓你知道,小'hh'是12小時格式,大寫'HH'是24小時格式。而且由於他在任務中指定他需要20.30才能轉換爲8:30:00,這意味着他需要12小時的格式。所以我再次說明我是正確的。所以根據這個問題,我在所有方面都是正確的 –

+0

嗯......不知道「HH:和......」的事情......它的好學習。我只是誤解了這個問題。謝謝,我的道歉。 –

0

嘗試,如果這個工程:

[dateFormatter setDateFormat:@"HH:mm 'on' dd MMMM YYYY"]; 
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]]; 
0

您使用計算秒的公式是錯誤的。

您可以使用類似

double t = 20.30; 

int hours = (int) floor(t); 
int minutes = (int) ((t-hours)*100); 

int seconds = hours*60*60 + minutes*60; 

NSLog(@"%@",[NSDate dateWithTimeIntervalSinceNow:seconds]); 
0

試試這個:

double time = 20.30; 
NSString *strTime = [NSString stringWithFormat:@"%.2f",time]; 
NSArray *values = [strTime componentsSeparatedByString:@"."]; 

double hours = [values objectAtIndex:0]; 
double min = [values objectAtIndex:1]; 

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:(hours*60*60)+mins*60]; 
NSLog(@"Time: %@", [dateFormat stringFromDate:date]); 
0

試試這個

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
dateFormat.dateFormat = @"HH:mm"; 
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]]; 

double time = 20.30; 
NSString* timeString=[NSString stringWithString:[[NSString stringWithFormat:@"%.2f",time]stringByReplacingOccurrencesOfString:@"." withString:@":"]]; 

NSDate *date = [dateFormat dateFromString:timeString]; 
[email protected]"hh:mm:ss"; 
NSLog(@"Time: %@", [dateFormat stringFromDate:date]); 

這轉換20.30到08:30:00