请帮我获取当地日期和一天的开始,我的意思是午夜。为了得到当地的日期我使用下面的代码,但我不认为这是正确的Swift获取本地日期实例
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)
这看起来像另一个“NSDate的描述总是使用GMT”的副本。请参阅http://stackoverflow.com/questions/8466744/getting-date-from-nsdate-date-off-by-a-few-hours。 –
请注意,根据您当地的时区,“startToday”设置为午夜,格林威治可能是21:00。 –
电脑位置或当地日期的任何地方的当地日期?而对于一天的开始,你想要什么时区的结果呢?世界标准时间? – MirekE