gpt4 book ai didi

ios - 计算今天和 future 日期之间的天数和小时数

转载 作者:行者123 更新时间:2023-11-28 19:07:52 29 4
gpt4 key购买 nike

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

我正在尝试计算当前日期/时间与 future 日期/时间的天数、小时数、分钟数和秒数的差异。

我做错了什么?首先,它看起来不像时区工作正常。第二个是小时、分钟和秒数。

结果:当前日期:2013-09-12 05:42:24 +0000(应为太平洋标准时间晚上 10:43) future 日期: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 *difference = [[NSCalendar currentCalendar] components: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);

最佳答案

试试这个,

 - (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;
}

关于ios - 计算今天和 future 日期之间的天数和小时数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18756242/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com