我在我的應用程序中有一個計時器。當我點擊退出buton時,計時器停止並以01:15:55的格式將值存儲到字符串中。我有一個數組來存儲這個字符串對象。如何在沒有日期的情況下在NSDate中存儲時間?
我想要的是,現在我想通過比較顯示這些值。所以我想首先我必須將字符串轉換爲NSDate,但我只有時間格式,不想存儲日期。
我該如何完成這項任務?任何建議?
編輯:代碼
NSInteger secondsSinceStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:sDate]; // sDate = when app get started
myAppDelegate.seconds = secondsSinceStart % 60;
myAppDelegate.minutes = (secondsSinceStart/60) % 60;
myAppDelegate.hours = secondsSinceStart/(60 * 60);
NSString *result = nil;
if (myAppDelegate.hours > 0)
{
result = [NSString stringWithFormat:@"%02d:%02d:%02d", myAppDelegate.hours, myAppDelegate.minutes, myAppDelegate.seconds];
}
else
{
result = [NSString stringWithFormat:@"%02d:%02d", myAppDelegate.minutes, myAppDelegate.seconds];
}
NSString *tempDateString = [NSString stringWithFormat:@"%d:%d:%d",[myAppDelegate hours],[myAppDelegate minutes],[mogsApp seconds]];
現在我想tempDateString轉換成NSDate的,所以我可以用類似的比較對象。可能嗎 ?
謝謝...
雅我通過簡單的劃分和餘數得到秒,分和小時。我得到字符串像0:0:9現在如何將此字符串轉換爲NSDate,以便我可以比較它? – Maulik 2011-06-01 06:37:28
@Maulik:使用[NSScanner](http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSScanner_Class/Reference/Reference.html)解析字符串並將其轉換到NSTimeInterval(這只是一個浮動,BTW)。想知道如何做到這一點,看看這個問題[如何將時間值的NSString表示轉換爲包含小時和分鐘的兩個NSInteger?](http://stackoverflow.com/questions/3432260/how-可以-I-轉換-A-的NSString-表示對的一 - 時間 - 值 - 到 - 雙nsintegers)。 – DarkDust 2011-06-01 06:39:48
@darkDust:我只想做一下這個過程。當我準備好秒鐘,分鐘和小時。我想知道我可以將它轉換成NSDate而不插入日期嗎? – Maulik 2011-06-01 06:49:49