如何在两个日期之间检查当前日期是否在IOS中

时间:2014-10-02 10:21:12

标签: ios objective-c nsdateformatter

我试图找出当前日期是否在两个给定日期之间。首先我将这两个日期转换为当前日期格式,即:2014-10-02 06:45:37 +0000

            NSComparisonResult result,restult2;

            NSString *startDateStr=@"10/04/2014 06:03 AM";
            NSDateFormatter *startdf=[[NSDateFormatter alloc] init];
            [startdf setDateFormat:@"MM/dd/yyyy hh:mm a"];
            NSDate *startDate12=[startdf dateFromString:startDateStr];
            NSString *startStr=[startdf stringFromDate:startDate12];
            [startdf setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
            NSDate *startDate1=[startdf dateFromString:startStr]; //here startDate1 is nil

            NSString *endDateStr=@"10/07/2014 03:03 AM";
            NSDateFormatter *enddf=[[NSDateFormatter alloc] init];
            [enddf setDateFormat:@"MM/dd/yyyy hh:mm a"];
            NSDate *endDate12=[enddf dateFromString:endDateStr];
            NSString *endStr=[enddf stringFromDate:endDate12];
            [enddf setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
            NSDate *endDate1=[startdf dateFromString:endStr]; //here endDate1 is nil

            NSDate *date = [NSDate date];

            BOOL isBetween=[MyViewController date:date isBetweenDate:startDate1 andDate:endDate1];

            if (isBetween)
            {
                NSLog(@"@@@@@YES");
            }

+ (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate
{
    if ([date compare:beginDate] == NSOrderedAscending)
        return NO;

    if ([date compare:endDate] == NSOrderedDescending)
        return NO;

    return YES;
}

请在我出错的地方提出任何建议。

提前致谢...!

4 个答案:

答案 0 :(得分:1)

你在代码中做的事情几乎没有做任何事情。 NSDate对象没有格式。它没有时区。它没有几个月,几天,几年。这只是一个时间点。然后,当您将该日期转换为字符串时,您需要提供一种格式(这是NSDateFormatter的用途)。

将您的代码更改为此类内容......

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy hh:mm a"];

NSString *startDateStr = @"10/04/2014 06:03 AM";
NSDate *startDate = [dateFormatter dateFromString:startDateStr];

NSString *endDateStr = @"10/07/2014 03:03 AM";
NSDate *endDate = [dateFormatter dateFromString:endDateStr];

NSDate *date = [NSDate date];

// use a function name that matches the convention...
if ([MyViewController isDate:date betweenStartDate:startDate andEndDate:endDate])
{
    NSLog(@"@@@@@YES");
}

我认为你的功能应该没问题。只是不要乱用日期。一旦你让它们停在那里并使用它们。

答案 1 :(得分:0)

从字符串中获取NSDate并进行比较。 NSDate不包含任何格式。这只是秒数。所以你不需要多次重新转换它,它容易出错。 还要仔细检查 dateFromString:方法是否返回nil。

答案 2 :(得分:0)

要验证日期是否属于定义的日期范围,您可以使用比较方法或查看以下代码:

NSDate *now = [NSDate date];
NSDate *yesterday = [now dateByAddingTimeInterval:-1*24*60*60];
NSDate *tomorrow = [now dateByAddingTimeInterval:+1*24*60*60];

//first condition
if([now compare:yesterday] == NSOrderedDescending && [now compare:tomorrow] == NSOrderedAscending)
    NSLog(@"now > yesterday and now < tomorrow");
else
    NSLog(@"now is outside yesterday and tomorrow");

NSDate *weekAgo = [now dateByAddingTimeInterval:-7*24*60*60];

//second condition
if([weekAgo compare:yesterday] == NSOrderedDescending && [weekAgo compare:tomorrow] == NSOrderedAscending)
    NSLog(@"weekAgo > yesterday and weekAgo < tomorrow");
else
    NSLog(@"weekAgo is outside yesterday and tomorrow");

第一个条件导致

2014-10-02 14:45:21.791 dateTest [95595:8857503] now&gt;昨天和现在&lt;明天

第二个条件导致

2014-10-02 14:45:24.425 dateTest [95595:8857503] weekAgo在昨天和明天之外

答案 3 :(得分:-1)

以毫秒为单位转换两个日期

long firstDate = [@(floor([startDate1 timeIntervalSince1970] * 1000)) longLongValue];
long secondDate = [@(floor([endDate1 timeIntervalSince1970] * 1000)) longLongValue];

然后还要检查您要检查的日期,并检查该值是否大于firstDate且小于“secondDate&#39;