有很多方法来实现这个。就我个人而言,如果可以避免使用字符串,我不喜欢与字符串一起工作。我宁愿处理日期组件。
以下是创建8:00和16:30日期的代码,并将日期进行比较,以查看当前日期/时间是否落在该范围内。
虽然比其他人的代码更长,但我认为学习如何使用日历进行计算是值得的:
编辑#3:
这个答案是很久以前的。我会保留旧答案,但这里是当前的解决方案:
@CodenameDuchess的答案使用了一个系统函数,date(bySettingHour:minute:second:of:matchingPolicy:repeatedTimePolicy:direction:)
使用那个函数,代码可以简化为这样:
import UIKit
let calendar = Calendar.current
let now = Date()
let eight_today = calendar.date(
bySettingHour: 8,
minute: 0,
second: 0,
of: now)!
let four_thirty_today = calendar.date(
bySettingHour: 16,
minute: 30,
second: 0,
of: now)!
if now >= eight_today &&
now <= four_thirty_today
{
print("The time is between 8:00 and 16:30")
}
为了历史完整性,以下是旧的(Swift 2)答案:
这段代码使用 Calendar 对象获取当前日期的日/月/年,并添加所需的时/分组件,然后生成这些组件对应的日期。
import UIKit
extension NSDate
{
func dateAt(#hours: Int, minutes: Int) -> NSDate
{
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
println("Now = \(self)")
let date_components = calendar.components(
NSCalendarUnit.CalendarUnitYear |
NSCalendarUnit.CalendarUnitMonth |
NSCalendarUnit.CalendarUnitDay,
fromDate: self)
date_components.hour = hours
date_components.minute = minutes
date_components.second = 0
let newDate = calendar.dateFromComponents(date_components)!
return newDate
}
}
extension NSDate: Equatable {}
extension NSDate: Comparable {}
public func ==(lhs: NSDate, rhs: NSDate) -> Bool
{
return lhs.timeIntervalSince1970 == rhs.timeIntervalSince1970
}
public func <(lhs: NSDate, rhs: NSDate) -> Bool
{
return lhs.timeIntervalSince1970 < rhs.timeIntervalSince1970
}
public func >(lhs: NSDate, rhs: NSDate) -> Bool
{
return lhs.timeIntervalSince1970 > rhs.timeIntervalSince1970
}
public func <=(lhs: NSDate, rhs: NSDate) -> Bool
{
return lhs.timeIntervalSince1970 <= rhs.timeIntervalSince1970
}
public func >=(lhs: NSDate, rhs: NSDate) -> Bool
{
return lhs.timeIntervalSince1970 >= rhs.timeIntervalSince1970
}
let now = NSDate()
let eight_today = now.dateAt(hours: 8, minutes: 0)
let four_thirty_today = now.dateAt(hours:16, minutes: 30)
if now >= eight_today &&
now <= four_thirty_today
{
println("The time is between 8:00 and 16:30")
}
编辑:
这个答案中的代码已经在Swift 3中有了很大的改变。
现在不再使用NSDate
,而是使用本地的Date
对象,Date
对象默认就是可比较和相等的。
因此,我们可以摆脱Equatable
和Comparable
扩展以及<
,>
和=
操作符的定义。
然后,我们需要对dateAt
函数中的语法进行一些微调,以遵循Swift 3的语法。Swift 3中新的扩展如下所示:
Swift 3版本:
import Foundation
extension Date
{
func dateAt(hours: Int, minutes: Int) -> Date
{
let calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
var date_components = calendar.components(
[NSCalendar.Unit.year,
NSCalendar.Unit.month,
NSCalendar.Unit.day],
from: self)
date_components.hour = hours
date_components.minute = minutes
date_components.second = 0
let newDate = calendar.date(from: date_components)!
return newDate
}
}
let now = Date()
let eight_today = now.dateAt(hours: 8, minutes: 0)
let four_thirty_today = now.dateAt(hours: 16, minutes: 30)
if now >= eight_today &&
now <= four_thirty_today
{
print("The time is between 8:00 and 16:30")
}
println("\(someDate)")
的语法似乎在设备上以默认区域设置显示。) - Duncan C