返回不同值的 NSCalendar 组件
NSCalendar components returning different values
我有 2 个日期要比较。将来的日期和当前日期。当我比较它们时,我想检索它们在小时、分钟和秒方面的差异。我相信我必须执行此操作的代码是正确的,但是,根据我请求的组件,我得到不同的值 returned。
我在所有示例中使用以下日历:
let calendar = NSCalendar.currentCalendar()
甲:
let dateComponents = calendar.components([NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Second, NSCalendarUnit.Minute], fromDate: NSDate(), toDate: date, options: [])
乙:
let dateComponents2 = calendar.components(.Hour, fromDate: NSDate(), toDate: date, options: [])
因此,对于上面的示例,dateComponents.hour
不 等于 dateComponents2.hour
,dateComponents2.hour
return 是正确的值。
更有趣的是下面的案例:
甲:
let dateComponents = calendar.components([NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Second, NSCalendarUnit.Minute], fromDate: NSDate(), toDate: date, options: [])
乙:
let dateComponents2 = calendar.components(.Minute, fromDate: NSDate(), toDate: date, options: [])
现在,dateComponents.minute
不等于dateComponents2.minute
,但现在dateComponents.minute
return是正确的值。
为什么会这样?我同时请求多个组件会影响 return 值吗?
编辑
以下是使用以下日期的一些示例。 return 值显示为小时、分钟、秒:
2015-11-29 10:59:00 +0000
2015-11-28 07:57:20 +0000
使用日期组件:
3、1、39
使用 dateComponents2(每个组件一个):
27、1621、97299
dateComponents2 中的小时值是正确的,dateComponents 中的分钟和秒值是正确的。
假设两个NSDate
对象相隔61秒,如果你同时得到分和秒,你得到1分1秒,但如果你只要求秒,你得到 61 秒:
let date1 = NSDate()
let date2 = date1.dateByAddingTimeInterval(61)
let calendar = NSCalendar.currentCalendar()
let components1 = calendar.components([.Minute, .Second], fromDate: date1, toDate: date2, options: [])
let components2 = calendar.components([.Second], fromDate: date1, toDate: date2, options: [])
print("\(components1.minute) minute and \(components1.second) second")
print("\(components2.second) seconds")
产生:
1 minute and 1 second
61 seconds
使用您的日期(并在计算中添加天数和小时数):
let dateString1 = "2015-11-28 07:57:20 +0000"
let dateString2 = "2015-11-29 10:59:00 +0000"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let date1 = formatter.dateFromString(dateString1)!
let date2 = formatter.dateFromString(dateString2)!
let calendar = NSCalendar.currentCalendar()
let components1 = calendar.components([.Day, .Hour, .Minute, .Second], fromDate: date1, toDate: date2, options: [])
let components2 = calendar.components([.Second], fromDate: date1, toDate: date2, options: [])
print("\(components1.day) day, \(components1.hour) hours, \(components1.minute) minute, and \(components1.second) seconds")
print("\(components2.second) seconds")
结果是:
1 day, 3 hours, 1 minute, and 40 seconds
97300 seconds
如果将其相乘,您会发现 1 天 3 小时 1 分 40 秒等于 97300 秒。
您还可以考虑使用 NSDateComponentsFormatter
创建经过时间的格式良好(和本地化)的字符串表示形式:
let componentsFormatter = NSDateComponentsFormatter()
componentsFormatter.allowedUnits = [.Day, .Hour, .Minute, .Second]
componentsFormatter.unitsStyle = .Full
print(componentsFormatter.stringFromDate(date1, toDate: date2))
// or just show the top two units (because if you're showing days/hours, you probably no longer care about minutes/seconds
componentsFormatter.maximumUnitCount = 2
print(componentsFormatter.stringFromDate(date1, toDate: date2))
// but if you want actual total number of seconds, then change `allowedUnits` to use only that
componentsFormatter.allowedUnits = [.Second]
print(componentsFormatter.stringFromDate(date1, toDate: date2))
这将显示三个可选字符串:
1 day, 3 hours, 1 minute, 40 seconds
1 day, 3 hours
97,300 seconds
我有 2 个日期要比较。将来的日期和当前日期。当我比较它们时,我想检索它们在小时、分钟和秒方面的差异。我相信我必须执行此操作的代码是正确的,但是,根据我请求的组件,我得到不同的值 returned。
我在所有示例中使用以下日历:
let calendar = NSCalendar.currentCalendar()
甲:
let dateComponents = calendar.components([NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Second, NSCalendarUnit.Minute], fromDate: NSDate(), toDate: date, options: [])
乙:
let dateComponents2 = calendar.components(.Hour, fromDate: NSDate(), toDate: date, options: [])
因此,对于上面的示例,dateComponents.hour
不 等于 dateComponents2.hour
,dateComponents2.hour
return 是正确的值。
更有趣的是下面的案例:
甲:
let dateComponents = calendar.components([NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Second, NSCalendarUnit.Minute], fromDate: NSDate(), toDate: date, options: [])
乙:
let dateComponents2 = calendar.components(.Minute, fromDate: NSDate(), toDate: date, options: [])
现在,dateComponents.minute
不等于dateComponents2.minute
,但现在dateComponents.minute
return是正确的值。
为什么会这样?我同时请求多个组件会影响 return 值吗?
编辑 以下是使用以下日期的一些示例。 return 值显示为小时、分钟、秒:
2015-11-29 10:59:00 +0000
2015-11-28 07:57:20 +0000
使用日期组件:
3、1、39
使用 dateComponents2(每个组件一个):
27、1621、97299
dateComponents2 中的小时值是正确的,dateComponents 中的分钟和秒值是正确的。
假设两个NSDate
对象相隔61秒,如果你同时得到分和秒,你得到1分1秒,但如果你只要求秒,你得到 61 秒:
let date1 = NSDate()
let date2 = date1.dateByAddingTimeInterval(61)
let calendar = NSCalendar.currentCalendar()
let components1 = calendar.components([.Minute, .Second], fromDate: date1, toDate: date2, options: [])
let components2 = calendar.components([.Second], fromDate: date1, toDate: date2, options: [])
print("\(components1.minute) minute and \(components1.second) second")
print("\(components2.second) seconds")
产生:
1 minute and 1 second
61 seconds
使用您的日期(并在计算中添加天数和小时数):
let dateString1 = "2015-11-28 07:57:20 +0000"
let dateString2 = "2015-11-29 10:59:00 +0000"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let date1 = formatter.dateFromString(dateString1)!
let date2 = formatter.dateFromString(dateString2)!
let calendar = NSCalendar.currentCalendar()
let components1 = calendar.components([.Day, .Hour, .Minute, .Second], fromDate: date1, toDate: date2, options: [])
let components2 = calendar.components([.Second], fromDate: date1, toDate: date2, options: [])
print("\(components1.day) day, \(components1.hour) hours, \(components1.minute) minute, and \(components1.second) seconds")
print("\(components2.second) seconds")
结果是:
1 day, 3 hours, 1 minute, and 40 seconds
97300 seconds
如果将其相乘,您会发现 1 天 3 小时 1 分 40 秒等于 97300 秒。
您还可以考虑使用 NSDateComponentsFormatter
创建经过时间的格式良好(和本地化)的字符串表示形式:
let componentsFormatter = NSDateComponentsFormatter()
componentsFormatter.allowedUnits = [.Day, .Hour, .Minute, .Second]
componentsFormatter.unitsStyle = .Full
print(componentsFormatter.stringFromDate(date1, toDate: date2))
// or just show the top two units (because if you're showing days/hours, you probably no longer care about minutes/seconds
componentsFormatter.maximumUnitCount = 2
print(componentsFormatter.stringFromDate(date1, toDate: date2))
// but if you want actual total number of seconds, then change `allowedUnits` to use only that
componentsFormatter.allowedUnits = [.Second]
print(componentsFormatter.stringFromDate(date1, toDate: date2))
这将显示三个可选字符串:
1 day, 3 hours, 1 minute, 40 seconds
1 day, 3 hours
97,300 seconds