如何快速从日期选择器获取开始和结束时间? [英] how to get start and end time from datepicker in swift?

查看:97
本文介绍了如何快速从日期选择器获取开始和结束时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我从日期选择器中选择的日期获取开始和结束时间.

i want to get start and end time from date which i select from datepicker.

这是我的代码

 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

but i am getting output like 2016-10-12 12:00:00

我的问题是如何获得这样的输出

my question is how to get how to get output like this

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)")

这篇关于如何快速从日期选择器获取开始和结束时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆