gpt4 book ai didi

iphone - 如何比较两个日期?

转载 作者:行者123 更新时间:2023-12-03 20:18:04 25 4
gpt4 key购买 nike

我有三个日期:(1) previousDate (2) currentDate (3) nextDate,我想检查 currentDate 是否晚于前一个日期且早于 nextDate。我该怎么做?

最佳答案

 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;
}

关于iphone - 如何比较两个日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2199488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com