我想仅显示 UIDatePickerView 开始日期日历180天,当用户在180天内选择任何开始日期时,他会点击结束日期按钮,因此日期选择器应显示仅89天开始日期和选定的开始日期不应该是结束日期
答案 0 :(得分:1)
// Add A Button And Give them Connection
@IBOutlet weak var DateSelect: UIButton!
// Add "UIDatePicker" to your project and Give them connection
@IBOutlet weak var fromedatepicker: UIDatePicker!
// . and Make an Action Button to "UIDatePicker"
@IBAction func fromedatepicker(_ sender: Any) {
let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
let currentDate = NSDate()
let dateComponents = NSDateComponents()
let minDate = calendar.date(byAdding: dateComponents as DateComponents, to: currentDate as Date, options: NSCalendar.Options(rawValue: 0))
dateComponents.month = 3 //or you can change month = day(90)
let maxDate = calendar.date(byAdding: dateComponents as DateComponents, to: currentDate as Date, options: NSCalendar.Options(rawValue: 0))
self.fromedatepicker.maximumDate = maxDate
self.fromedatepicker.minimumDate = minDate
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
let strDate = dateFormatter.string(from: fromedatepicker.date)
print(strDate)
DateSelect.DateSelect.titleLabel?.text = strDate
}
答案 1 :(得分:0)
您可以使用addingTimeInterval
的{{1}}方法执行此操作。
Date
现在datePicker.minimumDate = startDate?.addingTimeInterval(60 * 60 * 24 * 90) // 90 days interval offset
,DatePicker始终显示90天转发日期,然后开始日期。
如果您希望在endDate
的当前日期显示180天,那么您可以像下面一样管理startDate。
startDate
它只会在datePicker中显示180天的持续时间。
答案 2 :(得分:0)
如果您想从开始日期开始仅显示90天而不是:
@IBOutlet var datePicker: UIDatePicker!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
datePicker.datePickerMode = .date //To show only Date format for date picker
//Get current Date
var currentDate = Date()
let addDays: TimeInterval = (60 * 60 * 24 * 180) //Add 180 for initial Days
currentDate = currentDate.addingTimeInterval(addDays)
datePicker.minimumDate = Date() //Start with current date
datePicker.maximumDate = currentDate //End with current date + 180 days.
}
//Action for DatePicker
@IBAction func displayDay(_ sender: Any) {
let choosenDate = datePicker.date
//If you want to set Date format for selected picker date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy"
let selectedDate = dateFormatter.string(from: choosenDate)
print(selectedDate)
//You can use different datePicker object to set below endDate as +90 days because if you you same date picker it will always add selected date +90 days to date picker.
let addDays: TimeInterval = (60 * 60 * 24 * 89) //Add 89 Days
//Set Start Date as choosen Date
datePicker.minimumDate = choosenDate
//Set Max Date as choosen Date + 89 Days
datePicker.maximumDate = Date(timeInterval: addDays, since: choosenDate)
}
答案 3 :(得分:0)
var datePicker: UIDatePicker = UIDatePicker()
datePicker?.datePickerMode = .date
datePicker?.minimumDate = Date() // this to set the start date today
//this to set the date range you can change " byAdding: .year " to "byAdding: .month" if you went the range by month or to "byAdding: .day"
let nextDays = Calendar.current.date(byAdding: .year, value: 14, to: Date())
datePicker?.maximumDate = nextDays ?? Date() // this to set the maximum date