Swift:在DatePicker中设置自定义日期(仅日期) [英] Swift: Set custom Date(Day only) in DatePicker

查看:415
本文介绍了Swift:在DatePicker中设置自定义日期(仅日期)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索并尝试了两个小时。我有2个日期选择器。假设 fromDatePicker untilDatePicker 。我将 untilDatePicker 设置为当前日期(例如:17/06/17),我想将 fromDatePicker 设置为本月的第一个日期(例如:17/06/17),但我不知道要设置 untilDatePicker 。我已阅读,但它无济于事我很多。任何建议和回答都会对我有所帮助。提前致谢

I search and try this for a couple hour. I have 2 date picker. Let say fromDatePicker and untilDatePicker. I set the untilDatePicker to current date (ex: 12/06/17) and i want to set the fromDatePicker into first date of the current month (ex: 01/06/17) but i dont have any idea to set the untilDatePicker. I have read this but its not help me to much. Any sugest and answer will help for me. Thanks in Advance

推荐答案

这是swift3解决方案,以及如何在datePicker中设置当前月份的第一天,如果需要先获取其他月份的日期只需更改该月某天的日期变量即可。

This is swift3 solution and how you set first day of current month in datePicker , if you need to get first of other month just change date variable with some day from that month

    let date = NSDate()
    let calendar = NSCalendar.current
    let components = calendar.dateComponents([.year,.month], from: date as Date)
    let startOfMonth = calendar.date(from: components)
    self.datePicker.setDate(startOfMonth!, animated: true)

这篇关于Swift:在DatePicker中设置自定义日期(仅日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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