ios 如何将当前日期与iphone中的上一个日期进行比较?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13229142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:06:01  来源:igfitidea点击:

How to compare current date to previous date in iphone?

iosiphonexcodensdate

提问by Honey

I would like to compare the current date with another date, and if that is date is earlier than the current date, then I should stop the next action. How can I do this?

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

I have todays date in yyyy-MM-ddformat. I need to check this condition

我有今天的日期yyyy-MM-dd格式。我需要检查这个条件

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

Here if displaydateis less than todays date then it has to enter that condition.

在这里,如果displaydate小于今天的日期,则必须输入该条件。

回答by Nitin Gohel

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");

got your solution from this answer How to compare two dates in Objective-C

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

回答by Paresh Navadiya

Alternativeto @NNitin Gohel'sanswer.

替代@NNitin Gohel's回答。

Compare using NSTimeIntervalie NSDatetimeIntervalSince1970:

比较使用NSTimeIntervalie NSDatetimeIntervalSince1970

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

回答by Paras Joshi

Some methods of NSDateclass are:

NSDate类的一些方法是:

  1. isEarlierThanDate. // using this method you can find out the date is previous or not..
  2. isLaterThanDate
  3. minutesAfterDate.
  4. minutesBeforeDate. etc..
  1. isEarlierThanDate. // 使用这个方法你可以找出日期是不是以前的..
  2. isLaterThanDate
  3. minutesAfterDate.
  4. minutesBeforeDate. 等等..

also see this link with many methods of NSDate in iPhone SDK..

还可以查看此链接,其中包含 iPhone SDK 中的许多 NSDate 方法。

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

如何在现实世界中与 iphone-sdk 约会

Update

更新

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

use this bellow method for convert NSString date to NSDate format just paste this method in your.m file

使用此波纹管方法将 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;
}

after that when you want to use it just use like bellow..

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

NSDate *tempDate2 = [self convertStringToDate:yourStringDate];

and then try to compare like this..

然后尝试像这样比较..

if (tempDate2 > nowDate)

回答by btype

You can have a closer look at this Tutorial about NSDateFormatterand when you have your NSDate object you can compare it to another NSDate and get an NSTimeInterval which is the difference in seconds.

你可以仔细看看这个关于 NSDateFormatter 的教程,当你有你的 NSDate 对象时,你可以将它与另一个 NSDate 进行比较并获得一个 NSTimeInterval,它是秒的差异。

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