【问题标题】:How to compare current date to previous date in iphone?如何在iphone中比较当前日期和上一个日期?
【发布时间】:2012-10-25 02:43:52
【问题描述】:

我想将当前日期与另一个日期进行比较,如果该日期早于当前日期,那么我应该停止下一步操作。我该怎么做?

我有yyyy-MM-dd 格式的今天日期。我需要检查这种情况

if([displaydate text]<currentdate)
{
    //stop next action 
}

这里如果displaydate 小于今天的日期,那么它必须输入那个条件。

【问题讨论】:

    标签: ios iphone xcode nsdate


    【解决方案1】:
    NSDate *today = [NSDate date]; // it will give you current date
    NSDate *newDate = [dateFormatter dateWithString:@"xxxxxx"]; // your date 
    
    NSComparisonResult result; 
    //has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending
    
    result = [today compare:newDate]; // comparing two dates
    
    if(result==NSOrderedAscending)
        NSLog(@"today is less");
    else if(result==NSOrderedDescending)
        NSLog(@"newDate is less");
    else
        NSLog(@"Both dates are same");
    

    从这个答案How to compare two dates in Objective-C得到你的解决方案

    【讨论】:

    • 不等于我应该比较是否小于今天的日期?
    • 检查我的更新答案访问我的回答中的链接我绝对确定你得到了你的解决方案我的朋友
    • 在上面你给出的答案中,我已将其修改为 NSDate *newDate=[NSDate dateWithString:@"%@",[displaydate text]];但我得到 newDate 为 nil 即使有价值在显示日期,即 2011-11-02 ..你能告诉我吗?
    • 它的好家伙.. 它帮助了我
    • @NitinGohel 很棒的解决方案
    【解决方案2】:

    您可以仔细查看此Tutorial about NSDateFormatter,当您拥有 NSDate 对象时,您可以将其与另一个 NSDate 进行比较,并获得以秒为单位的 NSTimeInterval。

    NSDate *nowDate = [NSDate date];
    NSTimeInterval interval = [nowDate timeIntervalSinceDate:pastDate];
    

    【讨论】:

      【解决方案3】:

      替代@NNitin Gohel's答案。

      比较使用NSTimeInterval 即NSDate timeIntervalSince1970:

      NSTimeInterval *todayTimeInterval = [[NSDate date] timeIntervalSince1970];
      NSTimeInterval *previousTimeInterval = [previousdate timeIntervalSince1970];
      
      if(previousTimeInterval < todayTimeInterval)
         //prevous date is less than today
      else if (previousTimeInterval == todayTimeInterval)
         //both date are equal
      else 
         //prevous date is greater than today
      

      【讨论】:

      • 当我尝试你的代码时,我会出错。我必须以 yyyy-MM-dd 格式获取今天的日期,为此我这样做:now = [NSDate date]; NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease]; df.dateFormat = @"yyyy-MM-dd";现在我需要将它与 Datepicker 选择的日期进行比较,该日期也应该是 yyyy-MM-dd 格式。这就是我正在做的 NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease]; df.dateFormat = @"yyyy-MM-dd"; NSArray *temp=[[NSString stringWithFormat:@"%@",[df stringFromDate:DatePicker.date]] componentsSeparatedByString:@""];我该如何继续?
      【解决方案4】:

      NSDate类的一些方法是:

      1. isEarlierThanDate。 // 使用这个方法你可以知道日期是不是以前的..
      2. isLaterThanDate
      3. minutesAfterDate。
      4. minutesBeforeDate。等等。

      还可以在 iPhone SDK 中查看此链接以及 NSDate 的许多方法。

      how-to-real-world-dates-with-the-iphone-sdk

      更新

      //Current Date
          NSDate *date = [NSDate date];
          NSDateFormatter *formatter = nil;
          formatter=[[NSDateFormatter alloc] init];
          [formatter setDateFormat:@"yyyy-MM-dd"];
      

      使用下面的方法将 NSString 日期转换为 NSDate 格式,只需将此方法粘贴到 your.m 文件中

      - (NSDate *)convertStringToDate:(NSString *) date {
      NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
      NSDate *nowDate = [[[NSDate alloc] init] autorelease];
      [formatter setDateFormat:@"yyyy-MM-dd"];
      // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", date);
      date = [date stringByReplacingOccurrencesOfString:@"+0000" withString:@""];
      nowDate = [formatter dateFromString:date];
      // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", nowDate);
      return nowDate;
      }
      

      之后,当您想使用它时,只需像下面这样使用..

      NSDate *tempDate2 = [self convertStringToDate:yourStringDate];
      

      然后尝试像这样比较..

      if (tempDate2 > nowDate)
      

      【讨论】:

      • 我必须以 yyyy-MM-dd 格式获取今天的日期,为此我正在这样做:now = [NSDate date]; NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease]; df.dateFormat = @"yyyy-MM-dd";现在我需要将它与 Datepicker 选择的日期进行比较,该日期也应该是 yyyy-MM-dd 格式。这就是我正在做的 NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease]; df.dateFormat = @"yyyy-MM-dd"; NSArray *temp=[[NSString stringWithFormat:@"%@",[df stringFromDate:DatePicker.date]] componentsSeparatedByString:@""];我该如何继续?
      • 如何将此日期与日期选择器选择的日期进行比较,该日期也在 yyyy-MM-dd 中。我在 NSArray 中选择了日期选择器日期。
      • 是的,你有一个数组也很容易,你可以将它与 [yourArray objectAtIndex:intValue]; 进行比较// 这里给出整数值
      • @Joshi 我试过这个:now = [NSDate date]; NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease]; df.dateFormat = @"yyyy-MM-dd";如何将它放入某个变量然后进行比较,因为当我在格式化程序中看到它显示 然后我在文本字段中有这样的日期:2011-11-02。我该如何比较请告诉我跨度>
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      • 2020-08-12
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多