- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试计算从一个日期到另一个日期的天数,并且找到了两种计算方法。
第一个使用 NSDateComponents 如下:
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
fromDate:date1
toDate:date2
options:0];
然后检查[组件日]。
第二个使用timeInterval:
NSInteger timeInterval = [date2 timeIntervalSinceDate:date1];
然后得到timeInterval/3600/24
一种方法是否比另一种更好(可能是因为 timeInterval 方法缺乏精度)?
谢谢!
最佳答案
使用 dateComponent 比通过假设 timeInterval/3600/24
自己操作要好得多。
原因:
所有日子都没有 60*60*24 秒!!!
从夏令时过渡时,一天可以有 23 或 25 小时。
The hour (common symbol: h or hr) is a unit of measurement of time. In modern usage, an hour comprises 60 minutes, or 3,600 seconds. It is approximately 1/24 of a mean solar day.
An hour in the Universal Coordinated Time (UTC) time standard can include a negative or positive leap second, and may therefore have a duration of 3,599 or 3,601 seconds for adjustment purposes.
来源:Wikipedia Hour .
关于ios - NSDateComponents 与 timeIntervalSince,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23557117/
我正在尝试计算从一个日期到另一个日期的天数,并且找到了两种计算方法。 第一个使用 NSDateComponents 如下: NSCalendar *gregorianCalendar = [[NSCa
我正在尝试找出我们现在是多少毫秒。我找不到以毫秒为单位返回忽略日期的时间的方法,所以我想我可以根据 timeIntervalSince 1970 方法返回的值来计算它。 我这样做了: NSLog(@"
我有一个 NSManagedObject 对象: @NSManaged public var timestamp: NSDate 我需要两者之间的时间间隔,所以我实现了: let interval =
我是一名优秀的程序员,十分优秀!