我想存储在sqlite数据库timeintervalSince1970中。 如果我使用此代码:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm"
dateFormatter.timeZone = NSTimeZone.localTimeZone()
var initDate: NSDate = dateFormatter.dateFromString("23-03-2015 00:00")!
let timeInSeconds = initDate.timeintervalSince1970 // 1,427,324,400.0
当我将timeInSeconds传递给NSDate时:
let dateStore: NSDate = NSDate(timeIntervalSince1970: timeInSeconds)
如果我打印dateStore:
println(dateStore)
输出' 2015-03-22 23:00:00 + 0000'
timeInSeconds = 1427324400.0 - > ' 2015-03-22 23:00:00 + 0000',但要存储' 2015-03-23 00:00:00 + 0000'间隔应为1427328000.0
我不知道如何得到它。
当我使用此代码时:
let cal2: NSCalendar = NSCalendar.currentCalendar()
cal2.timeZone = NSTimeZone(name: "Europe/Madrid")!
var timeZoneComps: NSDateComponents = NSDateComponents()
timeZoneComps.day = 23
timeZoneComps.month = 3
timeZoneComps.year = 2015
timeZoneComps.hour = 0
timeZoneComps.minute = 0
timeZoneComps.second = 0
var date1: NSDate = cal2.dateFromComponents(timeZoneComps)!
println(date1)
输出println:2015-03-22 23:00:00 +0000
请帮忙吗?
答案 0 :(得分:0)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm"
let myDateMidnightLocalTime = dateFormatter.dateFromString("23-03-2015 00:00")! // Mar 23, 2015, 12:00 AM
let timeInSeconds = myDateMidnightLocalTime.timeIntervalSince1970 // 1,427,079,600.0
let dateStored = NSDate(timeIntervalSince1970: timeInSeconds) // Mar 23, 2015, 12:00 AM matches
dateStored.timeIntervalSince1970 // 1,427,079,600.0 matches