9

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-dd format. I need to check this condition

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

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

IPS Brar
  • 346
  • 1
  • 14
Honey
  • 2,840
  • 11
  • 37
  • 71

4 Answers4

35
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

Community
  • 1
  • 1
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
1

Alternative to @NNitin Gohel's answer.

Compare using NSTimeInterval ie 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
Community
  • 1
  • 1
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • When I try ur code im getting err.I have to get todays date in yyyy-MM-dd format and for that Im doing like this : now = [NSDate date]; NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease]; df.dateFormat = @"yyyy-MM-dd"; and now I need to compare this with a Datepicker selected date which should also be in yyyy-MM-dd format..This is what Im doing NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease]; df.dateFormat = @"yyyy-MM-dd"; NSArray *temp=[[NSString stringWithFormat:@"%@",[df stringFromDate:DatePicker.date]] componentsSeparatedByString:@""];How can I proceed further? – Honey Nov 05 '12 at 10:04
0

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

NSDate *nowDate = [NSDate date];
NSTimeInterval interval = [nowDate timeIntervalSinceDate:pastDate];
btype
  • 1,593
  • 19
  • 32
0

Some methods of NSDate class are:

  1. isEarlierThanDate. // using this method you can find out the date is previous or not..
  2. isLaterThanDate
  3. minutesAfterDate.
  4. minutesBeforeDate. etc..

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

how-to-real-world-dates-with-the-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

- (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)
Aamir
  • 16,329
  • 10
  • 59
  • 65
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • I have to get todays date in yyyy-MM-dd format and for that I m doing like this : now = [NSDate date]; NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease]; df.dateFormat = @"yyyy-MM-dd"; and now I need to compare this with a Datepicker selected date which should also be in yyyy-MM-dd format..This is what I m doing NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease]; df.dateFormat = @"yyyy-MM-dd"; NSArray *temp=[[NSString stringWithFormat:@"%@",[df stringFromDate:DatePicker.date]] componentsSeparatedByString:@""];How can I proceed further ? – Honey Nov 05 '12 at 10:00
  • how can I compare this date with the datepickers selected date which is also in yyyy-MM-dd.I took datepickers date in NSArray . – Honey Nov 05 '12 at 10:07
  • see this answer.. http://stackoverflow.com/questions/949416/how-to-compare-two-dates-in-objective-c – Paras Joshi Nov 05 '12 at 10:08
  • yes its also easy you have an array you can compare it with [yourArray objectAtIndex:intValue]; // here give integer value – Paras Joshi Nov 05 '12 at 10:09
  • @Joshi I have tried this : now = [NSDate date]; NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease]; df.dateFormat = @"yyyy-MM-dd"; How to take it in a some variable and then compare because when i see in formatter it is showing and then I have date in textfield like this :2011-11-02 .How can I compare Please tell me – Honey Nov 05 '12 at 10:37