我有两种字符串格式:
timeOne = "12:58 AM"
timeTwo = "1:05 PM"
我将它们转换为NSDate,就像这样
func convertTime(tim: String) -> NSDate{
let strDate = tim
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm a"
//converts into NSDate so we can find the time remaining
return (dateFormatter.dateFromString(strDate))!
}
let tOne = convertTime(timeOne)
let tTwo = convertTime(timeTwo)
转换tOne和tTwo输出我
tOne = 2000-01-01 00:58:00 +0000
tTwo = 2000-01-01 12:05:00 +0000
我比较了两次,但我希望它返回7分钟,而我得到'667' 要比较的代码是:
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.Minute, fromDate: tOne, toDate: tTwo, options: [])
print(components)
打印出来
667
然而,如果两个时间都是相同的AM或PM,则它可以工作 我的意思是,让我们说
tOne = "1:06 PM"
tTwo = "1:15 PM"
运行比较代码,它成功输出“9”