将Double转换为NSDate时间,以便在Parse上查询

时间:2017-03-16 20:17:50

标签: swift double nsdate

我有两个双打,并且需要使用与两个双打匹配的时间间隔查询NSDate。如何将Double转换为NSDate,时间与我的Double匹配? :)如果可能的话......

编辑:对不起,我有点急 - 道歉!我拥有的双打代表一天的时间,如8.00或17.00 ..所以我需要将那个Double转换为NSDate中的时间,如果这有意义的话:)

2 个答案:

答案 0 :(得分:0)

简单使用timeIntervalSince1970并将double值作为参数传递。

let date = NSDate(timeIntervalSince1970: doubleValue)

答案 1 :(得分:0)

我从你的答案中找到了一些解决方案。这就是我最终得到的结果,它完美无缺:

let date = NSDate()
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let newDate = cal.startOfDayForDate(date)

let secondsSince1970ToYesterdayAt23 = newDate.timeIntervalSince1970
let secondsInOneHour = 3600.0

let secondsSince1970FromMidnight = secondsSince1970ToYesterdayAt23 + secondsInOneHour

let secondsFromMidnightToStartTime = (secondsInOneHour * (slider?.lowerValue)!) + secondsSince1970FromMidnight
let dateWithStartTime = NSDate(timeIntervalSince1970: secondsFromMidnightToStartTime)

let secondsFromMidnightToEndTime = (secondsInOneHour * (slider?.upperValue)!) + secondsSince1970FromMidnight
let dateWithEndTime = NSDate(timeIntervalSince1970: secondsFromMidnightToEndTime)

print("Start \(dateWithStartTime)") // Returning 2017-03-18 08:00:00 +0000
print("End: \(dateWithEndTime)") // Returning 2017-03-18 17:00:00 +0000