(1) previousDate (2) currentDate (3) nextDate の 3 つの日付があります。currentDate が前の日付より後で、nextDate より前であるかどうかを確認したいと思います。それ、どうやったら出来るの?
質問する
8884 次
6 に答える
8
NSDateFormatter *df= [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
NSDate *dt1 = [[NSDate alloc] init];
NSDate *dt2 = [[NSDate alloc] init];
dt1=[df dateFromString:@"2011-02-25"];
dt2=[df dateFromString:@"2011-03-25"];
NSComparisonResult result = [dt1 compare:dt2];
switch (result)
{
case NSOrderedAscending: NSLog(@"%@ is greater than %@", dt2, dt1); break;
case NSOrderedDescending: NSLog(@"%@ is less %@", dt2, dt1); break;
case NSOrderedSame: NSLog(@"%@ is equal to %@", dt2, dt1); break;
default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;
}
于 2011-04-20T10:39:08.230 に答える
6
NSDateクラスを使用していると思います。
isEqualToDateを使用して、2 つの NSDate オブジェクトを比較できます。また、現在の日付を確認するためのEarlyDateとlaterDateは、前の日付よりも大きく、次の日付よりも小さくなっています。
于 2010-02-04T12:14:25.130 に答える
4
私は単にこれを使ってそれをチェックしました:
if ([[currentDate laterDate:nextDate] isEqualToDate:nextDate]) {
NSLog(@"currentDate is earlier than nextDate");
}
if ([[currentDate laterDate:previousDate] isEqualToDate:currentDate]) {
NSLog(@"currentDate is later then previousDate");
}
私にとってはうまくいきました!Thx @Luca Matteis のヒント「laterDate:」
于 2013-02-01T17:44:47.173 に答える
0
compare
次のメッセージを使用して 2 つの比較を実行するだけですNSDate
。
if ([previousDate compare:currentDate] == NSOrderedAscending &&
[nextDate compare:currentDate] == NSOrderedDescending) {
NSLog(@"current date is in between previous and next date (non-inclusive)");
}
于 2014-07-04T07:40:25.550 に答える