Objective c 目标c日期差混淆

Objective c 目标c日期差混淆,objective-c,nsdate,Objective C,Nsdate,我试图得到两个日期之间的天数,我的逻辑似乎适用于各种日期——比如2012年8月12日至2013年8月12日——如果我在计算中包括结束日,它将返回364天,365天。但是,如果我插入2013年8月8日至2013年8月12日,则得到0,如果将结束日添加到计算中,则得到1。不确定我在这里做错了什么: NSString* strNow = @"August 12, 2013"; NSString* strThen = @"August 8, 2012"; NSString* strNumOfDays

我试图得到两个日期之间的天数,我的逻辑似乎适用于各种日期——比如2012年8月12日至2013年8月12日——如果我在计算中包括结束日,它将返回364天,365天。但是,如果我插入2013年8月8日至2013年8月12日,则得到0,如果将结束日添加到计算中,则得到1。不确定我在这里做错了什么:

NSString* strNow = @"August 12, 2013";
NSString* strThen = @"August 8, 2012";

NSString* strNumOfDays = [dateManager getDateDifference:strThen :strNow];
方法

- (NSString*) getDateDifference : (NSString*) startDate : (NSString*) endDate {

    //convert strings to dates
    NSDate* dateStart = [self convertStringToDate:startDate];
    NSDate* dateEnd = [self convertStringToDate:endDate];

    //check to make sure the second date is later than the first

    //get the number of days between
    NSCalendar* gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents* components = [gregorianCalendar components:NSDayCalendarUnit fromDate:dateStart toDate:dateEnd options:0];

    //the plus one is for including the endDate in the count
    return [NSString stringWithFormat:@"%i", [components day]+1];
}
将字符串转换为日期方法

- (NSDate*) convertStringToDate : (NSString*) strToConvert {

    NSError* error = nil;
    NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeDate error:&error];
    NSArray* arrDateMatches = [detector matchesInString:strToConvert options:0 range:NSMakeRange(0, [strToConvert length])];

    for (NSTextCheckingResult* match in arrDateMatches) {
        strToConvert =  [self convertDateToString:match.date];
    }

    NSDate* myDate = [dateFormatter dateFromString:strToConvert];
    return  myDate;
}
@Wain-转换正在按预期工作。 @Rob-我正在使用NSTextCheckingTypes,因为我不知道提交日期的格式-可能是2013年12月8日、2013年8月8日、2013年8月12日等。使用此功能:

- (NSDate*) convertStringToDate : (NSString*) strToConvert {

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateStyle = NSDateFormatterLongStyle;
    dateFormatter.timeStyle = NSDateFormatterNoStyle;
    NSDate* myDate = [dateFormatter dateFromString:strToConvert];
    return  myDate;

}
使用此函数代替convertStringToDate函数。

使用此函数:

- (NSDate*) convertStringToDate : (NSString*) strToConvert {

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateStyle = NSDateFormatterLongStyle;
    dateFormatter.timeStyle = NSDateFormatterNoStyle;
    NSDate* myDate = [dateFormatter dateFromString:strToConvert];
    return  myDate;

}
使用此函数代替convertStringToDate函数。

您可以简化convertStringToDate。您不应该从NSTextCheckingResult获取日期,将其转换为字符串,然后再将其转换回日期。你只需:

- (NSDate*) convertStringToDate : (NSString*) string
{
    NSError *error = nil;
    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeDate error:&error];
    if (error)
        NSLog(@"%s: dataDetectorWithTypes error: %@", __FUNCTION__, error);

    NSTextCheckingResult* match = [detector firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];

    return match.date;
}
我也不清楚你为什么要在日期差上加1。我倾向于:

- (NSString*) getDateDifferenceBetweenStart:(NSString*)startDate end:(NSString*) endDate
{
    //convert strings to dates
    NSDate* dateStart = [self convertStringToDate:startDate];
    NSDate* dateEnd = [self convertStringToDate:endDate];

    //get the number of days between
    NSCalendar* gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents* components = [gregorianCalendar components:NSDayCalendarUnit fromDate:dateStart toDate:dateEnd options:0];

    return [NSString stringWithFormat:@"%i", [components day]];
}
注意,我正在使用更常见的语法来标识参数的名称:

NSString *difference = [self getDateDifferenceBetweenStart:strThen end:strNow]);
因此,这样做,我得到了人们预期的结果:

2012年8月12日至2013年8月12日的结果为365。 2013年8月8日至2013年8月12日,结果为4。 还不完全清楚为什么你的例行程序会返回它所做的事情。可能是您没有向我们展示的convertDateToString方法,或者是您的日期格式化程序配置,我们也不知道您在那里做了什么。但是上面的代码不需要这两个选项,并且似乎可以按预期工作。

您可以简化convertStringToDate。您不应该从NSTextCheckingResult获取日期,将其转换为字符串,然后再将其转换回日期。你只需:

- (NSDate*) convertStringToDate : (NSString*) string
{
    NSError *error = nil;
    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeDate error:&error];
    if (error)
        NSLog(@"%s: dataDetectorWithTypes error: %@", __FUNCTION__, error);

    NSTextCheckingResult* match = [detector firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];

    return match.date;
}
我也不清楚你为什么要在日期差上加1。我倾向于:

- (NSString*) getDateDifferenceBetweenStart:(NSString*)startDate end:(NSString*) endDate
{
    //convert strings to dates
    NSDate* dateStart = [self convertStringToDate:startDate];
    NSDate* dateEnd = [self convertStringToDate:endDate];

    //get the number of days between
    NSCalendar* gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents* components = [gregorianCalendar components:NSDayCalendarUnit fromDate:dateStart toDate:dateEnd options:0];

    return [NSString stringWithFormat:@"%i", [components day]];
}
注意,我正在使用更常见的语法来标识参数的名称:

NSString *difference = [self getDateDifferenceBetweenStart:strThen end:strNow]);
因此,这样做,我得到了人们预期的结果:

2012年8月12日至2013年8月12日的结果为365。 2013年8月8日至2013年8月12日,结果为4。
还不完全清楚为什么你的例行程序会返回它所做的事情。可能是您没有向我们展示的convertDateToString方法,或者是您的日期格式化程序配置,我们也不知道您在那里做了什么。但是上面的代码不需要这两个选项,并且似乎按照预期工作。

是否检查convertStringToDate的输出及其引发的任何错误?是否检查convertStringToDate的输出及其引发的任何错误?