ios - 在 Objective-C 中获取当天的进度

标签 ios objective-c

我需要获取当天的进度(例如从早上 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/

相关文章:

ios - 如何使用带有 Alamofire 参数的数组?

ios - 设置 UITableViewCell backgroundView 和 selectedBackgroundView 在 iOS7 中不起作用

ios - 自动布局错误 Unable to simultaneously satisfy constraints with UIScrollView

ios - ViewController 在 didSelectRowAtIndexPath 中缓慢呈现

objective-c - 有没有人可以将 iOS 核心数据与外部库同步

iphone - Objective C/iOS 中 Ruby method_missing 的等价物

ios - 如何在 iOS 上创建宽度 = 最大子宽度的 StackView?

iphone - 多个UIButton打开相同的 View 但内容不同

ios - 'isSubclassOfClass' 在 NSObject 中定义时如何不是有效的选择器?

objective-c - 使用 alloc init 而不是 new