我正在開發鬧鐘。 我想比較現在的時間和setTime。是否可以僅在幾分鐘內進行比較?需要關於使用NSDate進行時間比較的建議
我的問題是NSDate會以秒爲單位進行比較,例如9:38:50不等於9:38:00。 如何在幾分鐘內比較?
我正在開發鬧鐘。 我想比較現在的時間和setTime。是否可以僅在幾分鐘內進行比較?需要關於使用NSDate進行時間比較的建議
我的問題是NSDate會以秒爲單位進行比較,例如9:38:50不等於9:38:00。 如何在幾分鐘內比較?
首先,將它們轉換爲UNIX時間戳忽略毫秒。
NSUInteger time1 = (NSUInteger)[date1 timeIntervalSince1970];
NSUInteger time2 = (NSUInteger)[date2 timeIntervalSince1970];
忽略秒。
time1 = time1 - (time1 % 60);
time2 = time2 - (time2 % 60);
現在,您可以安全地比較它們。
if (time1 == time2) {
NSLog(@"Bingo");
}
使用NSCalendar得到NSDateComponents你有興趣,然後可以很容易地比較這些。
雖然檢查當前日期是否晚於警報日期可能已足夠。如果是,並且警報沒有停止,即使計算機暫時失去電源或其他東西,也應該停止。
感謝您的建議查克 – RAGOpoR 2010-06-02 04:20:19
感謝您的建議iwat – RAGOpoR 2010-06-02 04:21:00