squimer / DatePickerDialog-iOS-Swift

Date picker dialog for iOS
MIT License
581 stars 177 forks source link

how to set dateFormatter? #56

Closed xs6615 closed 7 years ago

xs6615 commented 7 years ago

I have tried many ways, but cannot get the dateFormatter to work. Can someone show me how to set this up?

lfarah commented 7 years ago

Sure! Post your code here and I can help you!

xs6615 commented 7 years ago

I am using the basic code in the sample. The format I need is below. THANK YOU!

 @IBAction func datePickerTapped(sender: AnyObject) {
        let currentDate = Date()
        let dateFormatter = DateFormatter()

        dateFormatter.dateFormat = "yyyy-MM-dd"

        var dateComponents = DateComponents()
        dateComponents.month = -12
        let threeMonthAgo = Calendar.current.date(byAdding: dateComponents, to: currentDate)

        DatePickerDialog().show("Select a date", doneButtonTitle: "Done", cancelButtonTitle: "Cancel", minimumDate: threeMonthAgo, maximumDate: currentDate, datePickerMode: .date) { (date) in
            if let dt = date {
                self.diveDateTextfield.text = "\(dt)"
            }
        }
    }
lfarah commented 7 years ago
 @IBAction func datePickerTapped(sender: AnyObject) {
        let currentDate = Date()                
        var dateComponents = DateComponents()
        dateComponents.month = -12
        let threeMonthAgo = Calendar.current.date(byAdding: dateComponents, to: currentDate)

        DatePickerDialog().show("Select a date", doneButtonTitle: "Done", cancelButtonTitle: "Cancel", minimumDate: threeMonthAgo, maximumDate: currentDate, datePickerMode: .date) { (date) in
            if let dt = date {
                let formatter = NSDateFormatter()
                formatter.dateFormat = "yyyy-MM-dd"
                self.diveDateTextfield.text = "\(formatter.stringFromDate(date))"
            }
        }
    }
lfarah commented 7 years ago

I got the code from #30

lfarah commented 7 years ago

Closing this.... Feel free to reopen if it doesn't work

xs6615 commented 7 years ago

Works like a charm. I only had to make some slight modifications. I am posting only in case someone else needs it. THANK YOU!!!.

    @IBAction func datePickerTapped(sender: AnyObject) {
        let currentDate = Date()
        var dateComponents = DateComponents()
        dateComponents.month = -12
        let threeMonthAgo = Calendar.current.date(byAdding: dateComponents, to: currentDate)

        DatePickerDialog().show("Select a date", doneButtonTitle: "Done", cancelButtonTitle: "Cancel", minimumDate: threeMonthAgo, maximumDate: currentDate, datePickerMode: .date) { (date) in
            if date != nil {
                let formatter = DateFormatter()
                formatter.dateFormat = "yyyy-MM-dd"
                self.diveDateTextfield.text = "\(formatter.string(from: date!))"
            }
        }
    }