[英]Objective-c date difference confusion
我試圖獲得兩個日期之間的天數,並且我的邏輯似乎在各種日期工作正常 - 比如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年8月8日,2013年8月8日,2013年8月12日等。
您可以簡化convertStringToDate
。 您不應該費心從NSTextCheckingResult
獲取date
,將其轉換為字符串,然后將其轉換回日期。 你應該簡單地說:
- (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]);
因此,這樣做,我得到了預期的結果:
365
。 4
。 這並不完全清楚為什么你的例行程序返回它所做的事情。 它可能是你的convertDateToString
方法(你沒有告訴我們),或你的日期格式化程序的配置(再次,我們也不知道你在那里做了什么)。 但上面的代碼不需要其中任何一個,似乎按預期工作。
使用此功能:
- (NSDate*) convertStringToDate : (NSString*) strToConvert {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterLongStyle;
dateFormatter.timeStyle = NSDateFormatterNoStyle;
NSDate* myDate = [dateFormatter dateFromString:strToConvert];
return myDate;
}
而不是你的convertStringToDate函數使用此函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.