0

I am trying to compare two times in iOS. I have list of times in my array i just want to check current time is matching to the any one the array value. can any help how to do that. i searched whole internet i did't get any idea.

I saw this question answer but i can't get a exact result.

Community
  • 1
  • 1
Gurumoorthy Arumugam
  • 2,129
  • 1
  • 25
  • 40
  • Current time with how much precision? I doubt `[datesArray containsObject:[NSDate date]]` would work out of the box... –  May 21 '13 at 05:33
  • check this may be it gives u some idea. http://stackoverflow.com/questions/13229142/how-to-compare-current-date-to-previous-date-in-iphone/13229194#13229194 – Nitin Gohel May 21 '13 at 05:34
  • Did you try NSDate Compare method? if it returns NSOrderedSame the dates are the same – Vertig0 May 21 '13 at 05:36
  • In what form that time is stored in array, – rptwsthi May 21 '13 at 05:37
  • 1
    NSDate compare is almost never going to return NSOrderedSame as it compares all elements, including seconds, microseconds etc. This question is badly worded and needs more details to be answered appropriately. – Rog May 21 '13 at 05:37
  • 2
    You searched the [whole](http://developer.apple.com/library/mac/search/?q=compare+time) -- [entire](http://stackoverflow.com/search?q=%5Bobjc%5D+compare+time) -- [internet](http://www.google.com/search?ie=utf8&oe=utf8&q=objective-c+compare+times&nfpr=1) and didn't get _any_ ideas about how to compare times? I don't believe you at all. – jscs May 21 '13 at 05:47
  • @Rog but it depends on the format or not? – Vertig0 May 21 '13 at 05:57

3 Answers3

1

According to Apple documentation of NSDate compare:

Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date.

- (NSComparisonResult)compare:(NSDate *)anotherDate

Parameters anotherDate

The date with which to compare the receiver. This value must not be nil.

Return Value

If:

The receiver and anotherDate are exactly equal to each other, NSOrderedSame

The receiver is later in time than anotherDate, NSOrderedDescending

The receiver is earlier in time than anotherDate, NSOrderedAscending

In other words:

if ([date1 compare:date2]==NSOrderedSame) ...
Note that it might be easier to read and write this :

if ([date2 isEqualToDate:date2]) ...
Ahmed Z.
  • 2,329
  • 23
  • 52
1
-(NSString *)determineDateFromstring:(long long)date
{
    NSTimeInterval interval=date;
    NSDate *currentTime=[NSDate dateWithTimeIntervalSince1970:interval/1000];

    NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];

    NSNumber *myDateInString=[NSNumber numberWithLongLong:[[NSDate date] timeIntervalSince1970]*1000];
    NSTimeInterval inte=[myDateInString longLongValue];
    NSDate *todayTime=[NSDate dateWithTimeIntervalSince1970:inte/1000];



    NSCalendar *currentcalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [currentcalendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:todayTime];

    int currentday=[components day];
    int currentyear=[components year];

    NSCalendar *paramtercalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *parametercomponents = [paramtercalendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:currentTime];

    int currentday1=[parametercomponents day];
    int currentyear1=[parametercomponents year];


    if (currentday==currentday1) {
     Nslog(date are same);  

    }else if(currentyear==currentyear1) {
     Nslog(year are same);

    }else{

    }

}

if u want to check two date are equal or not then u can compare NStimeIntervals(long long values). if same then time ,date and year are same otherwise different.

I hope this answer will be the right to your question.......

Mahesh
  • 317
  • 1
  • 3
  • 17
0

Why don't you check the interval since 1970 and compare the integer value..

    long long int i = [[NSDate date] timeIntervalSince1970];

I hope this will help.Also if you have values in form of NSString then you first need to convert them to NSDate

        NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ssSSSSSS";  
        NSDate *d = [dateFormatter dateFromString:@"your date string"];
  • 1
    Instead, check the time interval between the two dates directly with `timeIntervalSinceDate:`. Also, an `NSTimeInterval` is a `double`, not an integer. – jscs May 21 '13 at 05:50