0

我试图在 iOS 中比较两次。我的数组中有时间列表,我只想检查当前时间是否与数组值中的任何一个匹配。可以帮助如何做到这一点。我搜索了整个互联网我没有得到任何想法。

我看到了这个问题的答案 ,但我无法得到确切的结果。

4

3 回答 3

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{

    }

}

如果你想检查两个日期是否相等,那么你可以比较 NStimeIntervals(long long 值)。如果相同,则时间、日期和年份相同,否则不同。

我希望这个答案对你的问题是正确的......

于 2013-05-21T06:15:58.737 回答
1

根据 NSDate 的 Apple 文档比较:

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

- (NSComparisonResult)compare:(NSDate *)anotherDate

参数 anotherDate

与接收者进行比较的日期。该值不能为零。

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]) ...
于 2013-05-21T05:36:19.850 回答
0

为什么不检查自 1970 年以来的间隔并比较整数值..

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

我希望这会有所帮助。此外,如果您有NSString形式的值,那么您首先需要将它们转换为NSDate

        NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ssSSSSSS";  
        NSDate *d = [dateFormatter dateFromString:@"your date string"];
于 2013-05-21T05:50:12.613 回答