简体   繁体   中英

objective c compare two date strings in NSDate

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.

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...

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM