IOS Swift 来自字符串输出的 NSDate GMT 午夜

IOS Swift NSDate from String output GMT midnigth

我想将 timeintervalSince1970 存储在 sqlite 数据库中。 如果我使用此代码:

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)

Output '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)

Output println: 2015-03-22 23:00:00 +0000

有什么帮助吗?

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