2

Hi I am having two date as string , that is assigned to two labels,

one label holds current date string, that is may 29, 2010

similarly the other date will be selected by user in this same format, i need to check the user date is present, past , or future. by comparing with current date string.

please provide some sample code.

Thanks in advance.

mac
  • 4,760
  • 7
  • 31
  • 33
  • http://stackoverflow.com/questions/994855/comparing-dates-in-iphone-programming – mac May 29 '10 at 10:51

2 Answers2

3

simple... Comparing Dates

  • – isEqualToDate:
  • – earlierDate:
  • – laterDate:
  • – compare:

These are all in NSDate documentation which u can access by pressing the help button on menu and then selecting Developer Documentation...type in search NSDate.then what u need is compare.....

IF AND ONLY IF u want it to be present...put a check as special case if the dd/MM/YYYY are same, say its a present value...OR USE isEqualToDate...... IF the comparison with current date gives a negative value....its future.. IF the comparison with current date gives a positive value....its past...

Izac Mac
  • 491
  • 3
  • 20
1

Here's the code I used to solve this problem:

[Lout setStringValue:mydates];
NSDate* dateDB = [NSDate date];
NSDate* date = [NSDate date];

//Create the dateformatter object
NSDateFormatter* formatterDate = [[[NSDateFormatter alloc] init] autorelease];

[formatterDate setDateFormat:@"MM-dd-yy HH:mm:ss"];

dateDB = [formatterDate dateFromString:value3];

NSLog([NSString stringWithFormat:@"date value = %@",[formatterDate stringFromDate:dateDB]]);

NSTimeInterval timeInt = [[NSDate date] timeIntervalSinceDate:dateDB];
timeInt = timeInt/60;

This code will give you elapsed time in minutes. (It will give a negative number if the "since" date is in the future.)

buildsucceeded
  • 4,203
  • 4
  • 34
  • 72
Izac Mac
  • 491
  • 3
  • 20