2011-09-14 20 views
2

目的Cで比較して、特定の時間と日付の期間がすでに他のものと重複しているかどうかを確認するにはどうすればよいですか?目的地Cの時間と日付を比較する

これは、最も一般的にその特定のタイムスロットが取り上げられているかどうかを確認するために予約/予約アプリケーションで使用されるなど

答えて

2

これだけ使う、あなたはNSDateとして格納されている日付を持っていると仮定すると:

[myDate timeIntervalSinceNow]; 

これはNSTimeIntervalを返します。これは秒単位の時間間隔を表す浮動小数点数です。それが負の場合は、あなたが比較している時間が前のものであることを意味します。

あなたの日付がNSStringとしてplistに保存されている場合は、NSDateに最初に変換する必要があります。NSDateFormatterを使用してください。

NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //Change this to your date format 
NSDate * date = [formatter dateFromString:yourString]; 
[formatter release]; 
6

..

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
      [dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss aa"]; 


NSDate *dateOne=[dateFormat dateFromString:@"01/09/2011 11:12:10 AM" ]; 
     NSDate *dateTwo = [NSDate date]; 

     switch ([dateOne compare:dateTwo]){ 
      case NSOrderedAscending: 
       //NSLog(@"NSOrderedAscending"); 
       break; 
      case NSOrderedSame: 
       //NSLog(@"NSOrderedSame"); 
      { 
break; 
      case NSOrderedDescending: 
       //NSLog(@"NSOrderedDescending"); 
       break; 
     } 

[dateFormat release]; 
を比較するために、これを試してみてください