1

How can I compare the dates only, not the time. I am using

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];

NSString *tempDate = @"2-2-2012"; //Dynamic Date
NSDate *dateString = [dateFormatter dateFromString:tempDate];
NSLog(@"%@",dateString);

It logs this: 2012-02-01 18:30:00 +0000

NSDate *now = [NSDate date];//Current Date
NSLog(@"%@",now);

It logs this: 2011-04-04 14:49:45 +0000

I want to compare Dynamic date and current date, I don't need time. I may not using the correct NSDateFormatter. Can anyone of you tell me how to do this? If I am not clear, please let me know.

Suppose I have to strings

date1 = 3-2-2011;
date2 = 4-5-2020;

I want to convert them in date, only after that I can compare them. Its not happening from my date Formatter. Please have a look.

Thanks!

Developer
  • 6,375
  • 12
  • 58
  • 92
  • possible duplicate of [How to determine if an NSDate is today?](http://stackoverflow.com/questions/2331129/how-to-determine-if-an-nsdate-is-today) – Caleb Apr 04 '11 at 15:16

3 Answers3

2

Simplest way is to compare date by converting it into string.

Sample Code is as shown below:

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

NSString *dateString = [formatter stringFromDate:date];
[formatter release];

   //Other Date say date2 is of type NSDate again
   NSString *date2String = [formatter stringFromDate:date2];

  //Comparison of Two dates by its conversion into string as below
 if([date2String isEqualToString:dateString])
 {
       //Your logic if dates are Equal
 }
 else if(![date2String isEqualToString:dateString])
 {
        //Your Logic if dates are Different
 }

EDIT:

Checkout this link.

Comparing dates

http://www.iphonedevsdk.com/forum/iphone-sdk-development/64625-how-compare-2-dates.html

Hope This Helps You. :)

Community
  • 1
  • 1
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
  • @PARTH: Thanks Parth, but i also need to check which date is older or newer and I think that is not possible by just comparing String. I need to compare dates. – Developer Apr 04 '11 at 15:44
  • @Harsh: You should have mentioned that in your question. Keep this in mind when you post some question next time. Because lack of necessary information may get you in a wrong direction as it did now. – Parth Bhatt Apr 04 '11 at 15:46
  • Checkout my answer I have added a link which may help you out. Check the EDIT section of my answer – Parth Bhatt Apr 04 '11 at 15:50
  • 1
    @PARTH: Thanks for your answer, I did it by some other means but I am accepting your answer as it is good for the comparison of dates. Thanks. – Developer Apr 06 '11 at 16:32
0
   NSDate *date = [NSDate date];
NSDateFormatter *formatter = nil;
formatter=[[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setLocale:[NSLocale autoupdatingCurrentLocale]];

NSString *dateString = [formatter stringFromDate:date];
[formatter release];
unexpectedvalue
  • 6,079
  • 3
  • 38
  • 62
0

Use NSCalendar and NSDateComponents to get a date components object. Then you can look at only those parts of the date that you care about.

If you're just trying to determine whether two dates are the same, regardless of time, one way to go is to use NSDate's -timeIntervalSinceDate: method. If the time interval returned is less than 86,400 seconds (i.e. 24 hours * 60 minutes * 60 seconds) then you can feel fairly sure that it's the same day. Changes related to such things as daylight savings time and leap seconds introduce some possibility of error... if that's a problem, go with NSDateComponents.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • @Harsh, there are plenty of examples in [the documentation](http://tinyurl.com/3gjqgd5). Don't skip [this one](http://tinyurl.com/44d2pzx). Please take a look at that first and come back if you need help with something specific. – Caleb Apr 04 '11 at 15:12