Swift 2.3 从当前日期开始的最近时间(优先或相等)
Swift 2.3 Nearest Hour (Superior or Equal) from current Date
我必须确定 H 和 H+3,其中 H >= 到最近的小时。
让我给你举几个例子:
if today's hour is 0h00 -> H = 0h and H+3 = 3h
if today's hour is 0h01 -> H = 1h and H+3 = 3h
if today's hour is 21h00 -> H = 21h and H+3 = 0h
if today's hour is 22h34 -> H = 23h and H+3 = 2h (day + 1)
我是 Swift 的新手,我知道如何在 Obj C 中获取最近的小时数,但是对于 Swift 我不确定。
有没有一种快速的方法来确定这 2 个变量 H 和 H+3 以便将两个标签的文本设置为任何给定时间。
我试过这个方法,但它给了我最近的时间,但不是>=。
func nextHourDate() -> NSDate? {
let calendar = NSCalendar.currentCalendar()
let date = NSDate()
let minuteComponent = calendar.components(NSCalendarUnit.Minute, fromDate: date)
let components = NSDateComponents()
components.minute = 60 - minuteComponent.minute
return calendar.dateByAddingComponents(components, toDate: date, options: [])
}
我正在 Swift 2.3
中开发
编辑:
看完answers/comments,下面是我开发的
func determineH() -> NSDate? {
let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let now = NSDate()
let components = gregorian.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: now)
if components.minute != 0 {
components.hour = components.hour + 1
components.minute = 0
}
let date = gregorian.dateFromComponents(components)!
return date
}
要获得下一个小时,您需要增加 1 小时,然后减少分钟数。如果您希望 nextHour(12:00PM) 为 12:00PM 而不是 1:00PM,则添加 59 分 59 秒而不是 1 小时。请注意,这不涉及毫秒;如果你关心这个,也把它归零:
extension Date {
func advancedToNextHour() -> Date? {
var date = self
date += TimeInterval(59*60+59)
let calendar = Calendar.current
let components = calendar.dateComponents([.second, .minute], from: date)
guard let minutes = components.minute,
let seconds = components.second else {
return nil
}
return date - TimeInterval(minutes)*60 - TimeInterval(seconds)
}
func advancedToNearest(hours: Int) -> Date? {
guard let next = advancedToNextHour(), hours > 0 else {
return nil
}
return next + TimeInterval(hours-1)*60*60
}
}
print(Date().advancedToNextHour())
print(Date().advancedToNearest(hours: 3))
根据您的代码:
检查当前小时的分钟数并返回到最后一个完整小时,然后才添加!?
我不会提供那个 arithmetic masterpiece
的代码,因为你已经差不多了
最佳:更多地使用 dateComponents
仅增加小时数 属性 并将分钟数重置为 0!?
comps.hours += 1
comps.minutes = 0
我必须确定 H 和 H+3,其中 H >= 到最近的小时。
让我给你举几个例子:
if today's hour is 0h00 -> H = 0h and H+3 = 3h
if today's hour is 0h01 -> H = 1h and H+3 = 3h
if today's hour is 21h00 -> H = 21h and H+3 = 0h
if today's hour is 22h34 -> H = 23h and H+3 = 2h (day + 1)
我是 Swift 的新手,我知道如何在 Obj C 中获取最近的小时数,但是对于 Swift 我不确定。
有没有一种快速的方法来确定这 2 个变量 H 和 H+3 以便将两个标签的文本设置为任何给定时间。
我试过这个方法,但它给了我最近的时间,但不是>=。
func nextHourDate() -> NSDate? {
let calendar = NSCalendar.currentCalendar()
let date = NSDate()
let minuteComponent = calendar.components(NSCalendarUnit.Minute, fromDate: date)
let components = NSDateComponents()
components.minute = 60 - minuteComponent.minute
return calendar.dateByAddingComponents(components, toDate: date, options: [])
}
我正在 Swift 2.3
中开发编辑:
看完answers/comments,下面是我开发的
func determineH() -> NSDate? {
let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let now = NSDate()
let components = gregorian.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: now)
if components.minute != 0 {
components.hour = components.hour + 1
components.minute = 0
}
let date = gregorian.dateFromComponents(components)!
return date
}
要获得下一个小时,您需要增加 1 小时,然后减少分钟数。如果您希望 nextHour(12:00PM) 为 12:00PM 而不是 1:00PM,则添加 59 分 59 秒而不是 1 小时。请注意,这不涉及毫秒;如果你关心这个,也把它归零:
extension Date {
func advancedToNextHour() -> Date? {
var date = self
date += TimeInterval(59*60+59)
let calendar = Calendar.current
let components = calendar.dateComponents([.second, .minute], from: date)
guard let minutes = components.minute,
let seconds = components.second else {
return nil
}
return date - TimeInterval(minutes)*60 - TimeInterval(seconds)
}
func advancedToNearest(hours: Int) -> Date? {
guard let next = advancedToNextHour(), hours > 0 else {
return nil
}
return next + TimeInterval(hours-1)*60*60
}
}
print(Date().advancedToNextHour())
print(Date().advancedToNearest(hours: 3))
根据您的代码:
检查当前小时的分钟数并返回到最后一个完整小时,然后才添加!?
我不会提供那个 arithmetic masterpiece
的代码,因为你已经差不多了
最佳:更多地使用 dateComponents
仅增加小时数 属性 并将分钟数重置为 0!?
comps.hours += 1
comps.minutes = 0