我有两个约会,我想比较。未来的日期和当前的日期。当我比较它们时,我想检索它们在小时、分钟和秒方面的差异。我相信我必须这样做的代码是正确的,但是,根据我请求的组件,我得到了不同的返回值。
我在所有例子中都使用以下日历:
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: [])B:
let dateComponents2 = calendar.components(.Hour, fromDate: NSDate(), toDate: date, options: [])因此,对于上面的示例,dateComponents.hour不等于dateComponents2.hour,dateComponents2.hour返回正确的值。
更有趣的是以下情况:
答:
let dateComponents = calendar.components([NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Second, NSCalendarUnit.Minute], fromDate: NSDate(), toDate: date, options: [])B:
let dateComponents2 = calendar.components(.Minute, fromDate: NSDate(), toDate: date, options: [])现在,dateComponents.minute并不等于dateComponents2.minute,但是现在dateComponents.minute返回正确的值。
为什么会发生这种事?我同时请求多个组件这一事实是否影响返回值?
下面是一些使用以下日期的编辑的示例。返回值显示为小时、分钟、秒:
2015-11-29 10:59:00 +0000
2015-11-28 07:57:20 +0000使用dateComponents:
3、1、39
使用dateComponents2 (每个组件一个):
27,1621,97299
dateComponents2中的小时值是正确的,而dateComponents中的分钟和秒值是正确的。
发布于 2015-11-28 08:04:14
假设这两个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分1秒 61秒
使用您的日期(并将天数和小时添加到计算中):
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天3小时1分40秒 97300秒
如果你把它乘以,你会看到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天,3小时,1分40秒 1天3小时 97,300秒
https://stackoverflow.com/questions/33968931
复制相似问题