我知道以下代码返回GMT时间,我们目前是GMT + 1。我正在尝试调整我的代码,以便返回正确的日期。如果我只是添加1小时的代码,那么当GMT + 1恢复为GMT时它将失败。有什么想法吗?
let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
gregorian.timeZone = NSTimeZone.localTimeZone()
var unitFlags : NSCalendarUnit = NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitWeekOfYear | NSCalendarUnit.CalendarUnitWeekday
gregorian.firstWeekday = 2
var components:NSDateComponents = gregorian.components(unitFlags, fromDate: date)
components.weekday = 2
components.hour = 0
components.minute = 0
components.second = 0
let resultDate:NSDate = gregorian.dateFromComponents(components)!
println("The resultDate is \(resultDate)")
答案 0 :(得分:0)
我使用以下几行调整了代码。如果有人能发现任何错误,我将不胜感激。代码确实有效。
var currentZone = NSTimeZone.localTimeZone()
if (currentZone.isDaylightSavingTimeForDate(date)) {
components.hour = +1
}