我想在日期比较中进行一些更改。
在我的应用程序中,我比较两个日期并获得天数的差异,但如果只有一天的差异,系统会显示0作为天数的差异。
NSDateFormatter *date_formater=[[NSDateFormatter alloc]init];
[date_formater setDateFormat:@"MMM dd,YYYY"];
NSString *now=[NSString stringWithFormat:@"%@",[date_formater stringFromDate:[NSDate date]]];
LblTodayDate.text = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@",now]];
NSDate *dateofevent = [[NSUserDefaults standardUserDefaults] valueForKey:@"CeremonyDate_"];
NSDate *endDate =dateofevent;
NSDate *startDate = [NSDate date];
gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int unitFlags = NSDayCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0];
int days = [components day];
我找到了一些解决方案,如果我们将时间设为00:00:00进行比较,那么它会给我正确答案,我是对还是错,我不知道。
答案 0 :(得分:4)
我通常会发现秒数差异并计算ceil(diffInSeconds / 86400)
。
答案 1 :(得分:4)
我认为这是不正确的时间00:00:00。
可能是你得到差异不到24小时,这就是为什么它四舍五入你和你0天。
亚历山大解决方案是正确的,所以使用像
这样的解决方案这也适合我。
NSDate *endDate=[dateFormat dateFromString:now];
NSTimeInterval interval = [CeremonyDate timeIntervalSinceDate:endDate];
int diff=interval/86400;//for converting seconds into days.
同样的问题,你可以在这里找到一个数字,但你可以用一种合理的方式来理清它。
答案 2 :(得分:0)
尝试使用此代码获取两个不同的日期和时间。
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:@"mm:ss"];
NSDate* firstDate = [dateFormatter dateFromString:@"04:45"];
NSDate* secondDate = [dateFormatter dateFromString:@"05:00"];
NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
NSLog(@"%f",timeDifference);
我希望这段代码对你有用。
答案 3 :(得分:0)
这是一个找到两个日期之间差异的完美解决方案
- (NSString *)calculateDuration:(NSDate *)oldTime secondDate:(NSDate *)currentTime
{
NSString *timeSincePost;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:oldTime toDate:currentTime options:0];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
if (year) {
timeSincePost = [NSString stringWithFormat:@"%ld%@", (long)year,[[kAppDelegate languageBundle] localizedStringForKey:@"y" value:@"" table:nil]];
}
else if (month) {
timeSincePost = [NSString stringWithFormat:@"%ld%@", (long)month,[[kAppDelegate languageBundle] localizedStringForKey:@"M" value:@"" table:nil]];
}
if(day) {
timeSincePost = [NSString stringWithFormat:@"%ld%@", (long)day,[[kAppDelegate languageBundle] localizedStringForKey:@"d" value:@"" table:nil]];
}
else if(hour) {
timeSincePost = [NSString stringWithFormat: @"%ld%@", (long)hour,[[kAppDelegate languageBundle] localizedStringForKey:@"H" value:@"" table:nil]];
}
else if(minute) {
timeSincePost = [NSString stringWithFormat: @"%ld%@", (long)minute,[[kAppDelegate languageBundle] localizedStringForKey:@"m" value:@"" table:nil]];
}
else if(second)
timeSincePost = [NSString stringWithFormat: @"%ld%@", (long)second,[[kAppDelegate languageBundle] localizedStringForKey:@"s" value:@"" table:nil]];
return timeSincePost;
}
并使用两个参数作为NSDate
调用上述函数NSString *duration = [self calculateDuration:postDate secondDate:[NSDate date]];
lblPostTime.text = duration;
note :: postDate是FirstDate&第二个日期是当前日期..