2017-08-01 173 views
2

我想要顯示UIDatePickerView僅180天的開始日期日曆,當用戶在180天內選擇任何開始日期時,他將點擊結束日期按鈕,以便日期選擇器只顯示89從開始日期和選定的開始日期開始的日期不應該是結束日期日期選擇器startDate和endDate Swift 3

回答

0

您可以使用addingTimeInterval方法Date來做到這一點。

datePicker.minimumDate = startDate?.addingTimeInterval(60 * 60 * 24 * 90) // 90 days interval offset 

現在爲endDate,總的DatePicker顯示裝置90日內提出日期,然後啓動日期。

如果你想在startDate的當前日期顯示180天,那麼你可以像下面一樣管理startDate。

datePicker.minimumDate = Date() //Today's date 
datePicker.maximumDate = Date().addingTimeInterval(60 * 60 * 24 * 180) //180 days forward time from today. 

它只會在datePicker中顯示180天的持續時間。

+0

謝謝你..讓我看看它的鐵鍋:)但由於很多提前 – King

+0

怎麼樣的開始日期??我必須只顯示180天? – King

+0

@King,檢查已更新的答案,其中180天的startDate持續時間已添加。 – Jaydeep

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) 
} 
+0

非常感謝,我真的很感激,但開始日期呢?我必須只顯示180天?並且,當用戶將從開始日期選擇任何日期,以便日期不應該在結束日期只有89天enddate日曆應顯示... – King

+0

@ King檢查更新答案。 –

+0

謝謝先生的幫助... – King

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  
} 
+0

非常感謝,我真的很感激,但開始日期呢?我必須只顯示180天?並且,當用戶將從開始日期中選擇任何日期,以便日期不應只在結束日期89天enddate日曆應顯示... – King

相關問題