ios - 获取两个日期之间的差异?
问题描述
我在 24 小时内有两个日期,例如 (dd-MMM-yyyy HH:mm)。我想知道这两个日期之间的区别。
我的代码:这只是获取日期,我还想要天数和剩余时间(以小时为单位)。(如 6 天 5 小时)
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
int numberOfDays = secondsBetween / 86400;
NSLog(@"There are %d days in between the two dates.", numberOfDays);
解决方案
NSDateComponentsFormatter
可以这样做
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.allowedUnits = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
NSLog(@"%@", [formatter stringFromTimeInterval:secondsBetween]);
推荐阅读
- ember.js - EmberError 找不到模板或视图
- angular - 如何使用 StreamController 在 AngularDart 中进行广播
- java - 如何在 RecyclerView 中突出显示视图
- sqlite - DatePicker 值在 SQLite Xamarin.Forms 中保存为错误日期
- cplex - 在 .dat 文件中输入数据
- java - 使用 spring boot 在 crud 中搜索
- sql - 找出一月份加入的员工人数
- ajax - 在 CodeIgniter 中使用 AJAX 从数据库中获取数据
- java - 单元测试 Java 线程
- html - 为谷歌搜索结果添加星星,没有 wordpress,只有 html css