2013-07-25 21 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