计算今天和未来日期之间的天数和小时数

时间:2013-09-12 05:45:14

标签: ios objective-c

**解决了。代码已更新以正常工作**

我正在尝试计算当前日期/时间和未来日期/时间的天数,小时数,分钟数和秒数之差。

我做错了什么?首先,它看起来并不像时区正常工作。其次,小时,分钟和秒的数量都很少。

结果: 当前日期:2013-09-12 05:42:24 +0000(应该是太平洋标准时间晚上10:43) 未来日期:2013-09-29 23:00:00 +0000(应该是太平洋标准时间下午4点)

天:17 营业时间:596523.235278(应为~425) 会议纪要:35791394(应为~25516) 秒:2147483647(应该是~1530960)

// Get today's Date
NSDate *todaysDate = [NSDate date];
NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *todaysComps = [gregorian1 components: NSUIntegerMax fromDate: todaysDate];
[todaysComps setTimeZone:[NSTimeZone timeZoneWithName:@"America/Vancouver"]];
NSDate *todayFinalDate = [gregorian1 dateFromComponents: todaysComps];
NSLog(@"Current date: %@", todayFinalDate);

// Set Future Date
NSDate *futureDate = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: futureDate];
[todaysComps setTimeZone:[NSTimeZone timeZoneWithName:@"America/Vancouver"]];
[components setHour: 16];
[components setMinute: 00];
[components setSecond: 00];
[components setDay: 29];
[components setMonth: 9];
[components setYear: 2013];
NSDate *newDate = [gregorian dateFromComponents: components];
NSLog(@"Future Date: %@", newDate);

//检查两者之间的时间     NSDateComponents *差异= [[NSCalendar currentCalendar]组件:NSDayCalendarUnit fromDate:todaysDate toDate:newDate options:0];     int dayDiff = [差异日];

difference = [[NSCalendar currentCalendar] components:NSHourCalendarUnit fromDate:todaysDate toDate:newDate options:0];
int hourDiff = [difference hour];

difference = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:todaysDate toDate:newDate options:0];
int minDiff = [difference minute];

difference = [[NSCalendar currentCalendar] components:NSSecondCalendarUnit fromDate:todaysDate toDate:newDate options:0];
int secDiff = [difference second];

self.daysLabel.text=[NSString stringWithFormat:@"%d",dayDiff];
self.hoursLabel.text=[NSString stringWithFormat:@"%d",hourDiff];
self.minutesLabel.text=[NSString stringWithFormat:@"%d",minDiff];
self.secondsLabel.text=[NSString stringWithFormat:@"%d",secDiff];

NSLog(@"\nDays: %i\nHours: %d\nMinutes: %i\nSeconds: %i",dayDiff,hourDiff,minDiff,secDiff);

4 个答案:

答案 0 :(得分:4)

试试这个,

 - (NSInteger)timeDifference:(NSDate *)fromDate ToDate:(NSDate *)toDate{

    HMLogger_Info(@"<INPUT PARAMETERS> FromDate: %@, ToDate: %@",fromDate,toDate);

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *differenceValue = [calendar components:NSHourCalendarUnit
                                                    fromDate:fromDate toDate:toDate options:0];
    HMLogger_Info(@"Calculated hour difference : %d",[differenceValue hour]);
    NSInteger hourDifference = [differenceValue hour];

    HMLogger_Info(@"<RETURN VALUE>: HourDifference: %d",hourDifference);
    return hourDifference;
}

答案 1 :(得分:0)

Try this:   

 NSDate *dt2 = [[NSDate alloc]init];
    NSString *dateStr=@"10/25/2013 11:22:33";
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
    dt2=[dateFormatter dateFromString:dateStr];
    NSDate *dt1=[NSDate date];
    NSUInteger unitFlags = NSDayCalendarUnit;
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [calendar components:unitFlags fromDate:dt1 toDate:dt2 options:0];
    int i=[components day]+1;
    int j=[components hour];
    int k=[components minute];
    NSLog(@"Days diff=%d, =%d, =%d",i,j,k);

答案 2 :(得分:0)

我认为这会对你有所帮助

NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSDayCalendarUnit|NSHourCalendarUnit fromDate:fromDate toDate:toDate options:0];
NSLog(@"The difference between from date and to date is %d days and %d hours",components.day,components.hour);

答案 3 :(得分:0)

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSString *firstDate = @"2014-06-05 12:55:00";

NSDate *date=[dateFormatter dateFromString:firstDate];

NSDate *currentDate = [NSDate date];

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:date toDate:currentDate options:0];

NSLog(@"The difference between from date and to date is %d days and %d hours and %d minute and %d second",components.day,components.hour,components.minute,components.second);

输出: DateBookApp [1179:70b]日期和日期之间的差异是0天22小时57分12秒