【问题标题】:Comparing uipickerdate to current date将 uipickerdate 与当前日期进行比较
【发布时间】:2013-04-06 02:23:29
【问题描述】:

这是我的两个日期选择器的代码,我需要使用 if 条件或开关将它们与当前日期进行比较。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter.timeStyle=NSDateFormatterShortStyle;
dateFormatter.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString=[dateFormatter stringFromDate:startTime.date];
NSLog(@"Start time is %@",dateTimeString);


NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
dateFormatter2.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter2.timeStyle=NSDateFormatterShortStyle;
dateFormatter2.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString2=[dateFormatter2 stringFromDate:endTime.date];
NSLog(@"End time is %@",dateTimeString2);

在我的 .h 中

@property (weak, nonatomic) IBOutlet UIDatePicker *startTime;
@property (weak, nonatomic) IBOutlet UIDatePicker *endTime;

我该怎么做?我是否需要将它们存储到 NSDate 或 NSTime,因为我真的只需要时间?

另外,Objective C 中是否有 If 范围?

谢谢,

【问题讨论】:

  • 我在您的代码中没有看到任何日期选择器。你在说什么日期选择器?
  • 感谢@matt 检查我的编辑并注意我的 .h、starttime.date 和 endtime.date 实际上是我的日期选择器
  • 好吧,我的答案仍然有效。

标签: ios objective-c xcode uipickerview uidatepicker


【解决方案1】:

只需添加这个:

if ([[NSDate date] isEqualToDate:startTime.date]) {
NSLog(@"currentDate is equal to startTime"); 
}

if ([[NSDate date] isEqualToDate:endTime.date]) {
NSLog(@"currentDate is equal to endTime"); 
}

如果要计算两个日期之间的间隔,请使用此方法

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate


if([startTime.date timeIntervalSinceDate:[NSDate date]] > 0)
{
  //start time greater than today
}

else if([startTime.date timeIntervalSinceDate:[NSDate date]] < 0)
{
    //start time less than today
}

else
{
   //both dates are equal
}

要知道应用程序何时进入后台使用通知,请在 viewDidLoad 中添加此语句

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnteredBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];

添加功能以在发布时满足通知

-(void)applicationEnteredBackground
{
   //do all the above steps here when you want.
}

并且在类的dealloc函数中移除通知的观察者

-(void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

【讨论】:

  • 优秀的直接答案!你知道如何将它链接到AppDelegateDidEnterBackground 方法吗?
  • @user1949873 表示你想要什么?,你想使用任何值吗?
  • 好吧,让我直说吧,你想在应用程序进入后台状态时做这一切吗?好的,然后看看我的编辑。
  • 你对上面的实现还好吗?,因为我没有在 appdelegate 的 DidEnterBackground 函数中做 insted 我用其他方式做,但或多或​​少是一样的。
【解决方案2】:

我在您的代码中看到的唯一日期是 startTime.dateendTime.date。我不知道你从哪里得到这些,但他们大概是 NSDates。另一方面,当前日期是[NSDate date]。因此,您可以使用 NSDate 的 compare: 方法或其他几种比较方法中的任何一种进行比较(请参阅 NSDate 文档)。

【讨论】:

  • NSComparisonResult res = [[NSDate date] compare: startTime.date]
【解决方案3】:

NSDate 实现了isEqualToDate:,它测试两个日期是否相同,精确到微秒。它还实现了earlierDate:laterDate:,您可以使用它们来构建范围检查。

您还可以构建一些帮助程序,让您使用timeIntervalSinceDate: 控制相等检查的灵敏度。没有日期范围对象,但您也可以在助手中添加中间检查...

@interfce NSDate (Comparison)

- (BOOL)isEqualToDate:(NSDate *)aDate within:(NSTimeInterval)tolerance;
- (BOOL)isBetween:(NSDate *)start and:(NSDate *)end within:(NSTimeInterval)tolerance;

@end

@implementation NSDate (Comparison)

- (BOOL)isEqualToDate:(NSDate *)aDate within:(NSTimeInterval)tolerance {

    NSTimeInterval difference = [self timeIntervalSinceDate:aDate];
    return fabs(difference) < tolerance;
}

- (BOOL)isBetween:(NSDate *)start and:(NSDate *)end within:(NSTimeInterval)tolerance {

    NSTimeInterval startDifference = [self timeIntervalSinceDate:start];
    if (startDifference < tolerance) return NO;

    NSTimeInterval endDifference = [end timeIntervalSinceDate:self];
    if (endDifference < tolerance) return NO;

    return YES;
}

@end

我没有测试过这些。

【讨论】:

  • NSDate 有比较方法,那么为什么要重新发明那个轮子呢? (不是批评,我想我可能会遗漏一些东西)
  • 这是一个有效的问题。我添加了测试以包含在我认为 OP 需要的范围内,以及降低测试灵敏度的想法。
【解决方案4】:

您也可以使用 NSDate 的 compare: 方法来执行以下操作,

 NSDateFormatter* df = [[NSDateFormatter alloc] init];
 [df setDateFormat:@"MM-dd-yyyy"];
 NSDate* date1 = [df dateFromString:@"12-23-2046"];

 NSDate *date2 = [NSDate date];

 if ([date1 compare:date2] == NSOrderedSame){
    NSLog(@"Same");
 }
 if ([date2 compare:date1]==NSOrderedAscending ) {
    NSLog(@"AScending");
 }
 if ([date2 compare:date1]== NSOrderedDescending) {
    NSLog(@"Descending");

 }

用你的 startTime,endTime 代替 date1,希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    相关资源
    最近更新 更多