'How to return a date using dateformatter in Swift?

I have a little problem and I don't know how can I solve it. I have a closedRange of dates which i'm using with a DatePicker.

How can I have a date with that format that I have in the code bellow ?

  var minMaxRange : ClosedRange<Date> {
        return dateStart...dateEnd
    }

I want the datePicker to start from the current date, and dateEnd is coming from the backend. dateEnd is a string so I do not have problem with this.

    var dateStart : Date {
        let isoDate = Date()
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
        dateFormatter.timeZone = .current
        return dateFormatter.date(from: isoDate)
    }
    
    var dateEnd : Date {
        let isoDate = syncViewModel.schedule[0].end
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
        dateFormatter.timeZone = .current
        return dateFormatter.date(from: isoDate) ?? Date.now
    }




Solution 1:[1]

You already have a Date... just write var dateStart = Date().

Then, you can format both dates (start and end) to a string when you show them on the screen, using:

dateFormatter.string(from: dateStart)

Solution 2:[2]

var minMaxRange: ClosedRange<Date> {
    .now ... dateEnd
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Leo Dabus