Swift获取本地日期实例

时间:2015-08-14 20:14:24

标签: swift datetime

请帮助我获取当地日期和当天的开始,我的意思是午夜。为了获得当地日期,我使用下面的代码,但我认为这不对

var calendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
        calendar!.timeZone = NSTimeZone.localTimeZone()
        let components = NSDateComponents()
        components.second = NSTimeZone.localTimeZone().secondsFromGMT
        let today = calendar!.dateByAddingComponents(components, toDate: NSDate(), options: nil)

但我不能在当天的午夜时分,它会在21.00返回时间

var comps = calendar!.components(NSCalendarUnit.YearCalendarUnit | .MonthCalendarUnit | .DayCalendarUnit | .HourCalendarUnit | .MinuteCalendarUnit | .SecondCalendarUnit, fromDate: today!)
        comps.hour = 0
        comps.minute = 0
        comps.second = 0

        let startToday = calendar!.dateFromComponents(comps)!

即使这回到21.00

calendar!.startOfDayForDate(today)

0 个答案:

没有答案