2013-07-25 22 views
-1

我從網絡服務獲得NSString的上午7:00和下午10:00。如何設置NSDate時間的日期部分以比較時間?

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"hh:mm a"]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CST"]]; 

    NSString *openDateString = (NSString*)[timeStringsArray2 objectAtIndex:0]; 
    NSString *closeDateString = (NSString*)[timeStringsArray2 objectAtIndex:1]; 

    NSDate *openDate = [dateFormatter dateFromString:openDateString]; 
    NSDate *closeDate = [dateFormatter dateFromString:closeDateString]; 

if ([self timeCompare:openDate until:closeDate]) { 
     NSLog(@"OPEN-timeCompare"); 
    } else { 
     NSLog(@"CLOSED-timeCompare"); 
    } 

這是比較方法:

-(BOOL)timeCompare:(NSDate*)date1 until:(NSDate*)date2{ 
    NSDate *date = [NSDate date]; 
    NSLog(@"open:%@ now:%@ close:%@", date1, date, date2); 
    return ([date1 compare:date] == NSOrderedAscending && [date2 compare:date] == NSOrderedDescending); 
} 

所以,當我比較這些值,我比較:

開:我使用此代碼塊把它們轉換成NSDate的2013 -07-26 12:00:00 +0000
現在:2013-07-27 03:50:30 +0000 關閉:2013-07-27 03:00:00 +0000 CLOSED-timeCompare

林不知道爲什麼,因爲現在它實際上950pm這是前10分鐘至10:00 PM。它不應該等於過去的關閉時間,即使它是UTC。

+0

你能澄清,其中日期1和date2是從哪裏來的?我沒有在你的代碼中看到它們。的 –

+0

可能重複[爲什麼NSDateFormatter dateFromString回報爲零?](http://stackoverflow.com/questions/17845994/why-does-nsdateformatter-datefromstring-return-nil) –

+0

它不是同樣的問題。請仔細閱讀! – marciokoko

回答

1

NSDateFormatter默認爲沒有年份的2000年。如果你希望你的Opendate裏和closeDate是在今年的0001,而不是2000年,試試這個:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
//Add year to expected date format 
[dateFormatter setDateFormat:@"yyyy hh:mm a"]; 
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CST"]]; 

NSString *openDateString = (NSString*)[timeStringsArray2 objectAtIndex:0]; 
NSString *closeDateString = (NSString*)[timeStringsArray2 objectAtIndex:1]; 

//make new strings with year 0001 + original time 
NSString *adjustedOpenDateString = [NSString stringWithFormat:@"%@ %@", @"0001", openDateString]; 
NSString *adjustedCloseDateString = [NSString stringWithFormat:@"%@ %@", @"0001", closeDateString]; 

NSDate *openDate = [dateFormatter dateFromString:adjustedOpenDateString]; 
NSDate *closeDate = [dateFormatter dateFromString:adjustedCloseDateString]; 

這應該設置日期格式找了一年,和0001添加年內要創建的字符串你的日期。不幸的是,我現在遠離Xcode,所以我不能保證沒有錯別字!

+0

謝謝,我把它的工作量增加到了2013年。但是因爲Im仍然使用01-01作爲默認的日期和月份,所以我假設測試仍然失敗。我將不得不獲得當前的日期和月份,以便能夠添加它們,然後進行比較。我認爲這是通過NSDateComponents完成的,對吧? – marciokoko

+0

這裏是展示瞭如何在午夜得到一個NSDate今天的鏈接,它可能會有所幫助:http://stackoverflow.com/questions/9040319/how-can-i-get-an-nsdate-object-for-今天,在午夜 –

+0

您可以使用NSDateFormatter來創建字符的形式顯示當前日期,時間SANS,然後用焦時間Concat的,並通過NSDateFormatter跑回來(用不同的格式)。但在某些時候,只需解碼開始/結束時間的小時和分鐘值並比較這些值,將其轉換爲NSDates更有意義。 –

1

使用NSDate的比較選擇:

if ([date1 compare:date2]==NSOrderedDescending) { 
    // date1 is after date2 
} 

也有earlierDate,timeIntervalSinceDate等

UPDATE: 比如現在測試是2日期間:

NSDate *now = [NSDate date]; 
    if ([date1 compare:now]==NSOrderedAscending && [date2 compare:now]==NSOrderedDescending) { 
     // now is between the 2 dates 
    } 
+0

Thx,但我需要知道現在是否在date1和date2之間以確定商店是打開還是關閉。 – marciokoko

+0

我添加了另一個示例來測試現在是否在兩個日期之間。 HTH –

+0

Thx但它仍然無法正常工作,我添加了即時比較日期的日誌。 – marciokoko