我想从我从日期选择器中选择的日期获取开始和结束时间。
这是我的代码
func handleDatePicker(sender: UIDatePicker) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
let startOfDay = NSCalendar.currentCalendar().startOfDayForDate(sender.date)
let components = NSDateComponents()
components.hour = 23
components.minute = 59
components.second = 59
let endOfDay = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: startOfDay, options: NSCalendarOptions(rawValue: 0))
}
但我得到的输出类似于 2016-10-12 12:00:00
我的问题是如何获得这样的输出
12-09-2016 00:00:00 AND 12-09-2016 23:59:59
最佳答案
尝试下面的代码:-
let calendar = NSCalendar(calendarIdentifier:NSCalendarIdentifierGregorian)
calendar?.timeZone = NSTimeZone(abbreviation: "GMT")!
let startOfDay = calendar?.startOfDayForDate(sender.date)
print("startDay \(startOfDay)")
let components = NSDateComponents()
components.hour = 23
components.minute = 59
components.second = 59
components.timeZone = NSTimeZone(abbreviation: "GMT")
let endOfDay = calendar?.dateByAddingComponents(components, toDate: startOfDay!, options: NSCalendarOptions(rawValue: 0))
print("end Day \(endOfDay)")
关于ios - 如何在swift中从日期选择器获取开始和结束时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39464326/