I'm trying to format a Date
object in Swift using DateFormatter
. I'm grabbing a Date object from a UIDatePicker in the format 2020-09-04T10:19:26+0000, and I want to reformat it to 2020-09-04 10:19.
From what I understand, DateFormatter
can only create a Date object from a string using the date(from: String)
method, or a string from a Date object using string(from: Date)
. Thus, for my seemingly simple use case, I would need two DateFormatters: One for converting my original date to a string, and a second one to format that string to my desired format and returning it as a Date object.
Is this really the "smart" way to re-format a Date object in Swift, or am I missing something?
edit:
I'm saving projects (which can have a due date) to a database. Later on, I plan on reading specific projects from the db based on their due date. The UIDatePicker returns an unformatted Date object (with seconds, offset and so on), but I only need the date, hour and minutes (exact to a 15 minute interval).