Swift:在DatePicker中设置自定义日期(仅日期) [英] Swift: Set custom Date(Day only) in 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屋!