Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 设置maximumDate时,日期选择器返回错误的日期_Ios_Swift_Uidatepicker_Swift4.2 - Fatal编程技术网

Ios 设置maximumDate时,日期选择器返回错误的日期

Ios 设置maximumDate时,日期选择器返回错误的日期,ios,swift,uidatepicker,swift4.2,Ios,Swift,Uidatepicker,Swift4.2,我有一个日期选择器,我将最大日期设置为两年前的日期 今天是2019年7月15日,距离2017年7月15日还有两年 现在,当我打开datepicker时,自动选取器从今天的日期(2019年7月15日)变为2017年7月15日,因为我已将maximumDate设置为2年前的日期 现在我不再滚动日期选择器,而是单击我读取选择器日期的“完成”按钮 print("mDoneAction===\(mDatePicker.date)") 这将按如下方式打印日期 2019-07-15 12:26:17 +00

我有一个日期选择器,我将
最大日期设置为两年前的日期

今天是2019年7月15日,距离2017年7月15日还有两年

现在,当我打开datepicker时,自动选取器从今天的日期(2019年7月15日)变为2017年7月15日,因为我已将maximumDate设置为2年前的日期

现在我不再滚动日期选择器,而是单击我读取选择器日期的“完成”按钮

print("mDoneAction===\(mDatePicker.date)")
这将按如下方式打印日期

2019-07-15 12:26:17 +0000
你知道为什么给我2019年而不是2017年作为提货人已经定在2017年7月15日而不是2019年7月15日了吗

open var date: Date // default is current date when picker created.
因此设置
maximumDate
不会影响
date
,您还需要设置
date

let d = UIDatePicker()
let year2Before = Calendar.current.date(byAdding: .year, value:-2, to: Date())!
d.date = year2Before
d.maximumDate = year2Before
来自文档

open var date: Date // default is current date when picker created.
因此设置
maximumDate
不会影响
date
,您还需要设置
date

let d = UIDatePicker()
let year2Before = Calendar.current.date(byAdding: .year, value:-2, to: Date())!
d.date = year2Before
d.maximumDate = year2Before

我猜这就是它的行为方式,尽管我无法找到任何明确讨论这种行为的文档。只需将
date
设置为与最大日期相同,它就应该按预期工作。我猜这就是它的行为方式,尽管我无法找到任何明确讨论此行为的文档。只需将
date
设置为与最大日期相同,即可正常工作。