我需要获取当天的进度(例如从早上 6 点到晚上 11 点)。我已经尝试了很多东西,到目前为止我唯一可以使用的代码是这段代码:
//Get the seconds between two days
//Get todays date
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
//Set todays date in a NSCalendar object
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *date1 = [gregorian dateFromComponents:components];
//Get tomorrows date
NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
components1.day++;
//Set tomorrows date in a NSCalendar object
NSDate *date2 = [gregorian dateFromComponents:components1];
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
NSLog(@"Test: %f", secondsBetween);
此代码将获取两天之间的总秒数(这并不重要,因为它是一个常量)。我如何获得两个不同时间之间的进度?由于用户将被允许更改它之间的计数时间,因此我需要能够在 NSTimer 中即时计算它。
编辑:我想知道如何使用 ProgressView 显示两次之间的百分比。例如。如果现在是上午 9 点并且时间是早上 6 点和中午 12 点,则 ProgressView 将为 50%。
最佳答案
两个日期之间的秒差是
[date2 timeIntervalSinceDate:date1];
这似乎就是您所需要的全部问题。
关于ios - 在 Objective-C 中获取当天的进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26089659/