Search code examples
swiftdateswift3nsdatensdateformatter

How do I make more than one date format in a single view in Swift?


I am trying to save data to a database and am using a date that shows seconds as the key but I want to show a smaller date in the value that is going to be displayed. The problem is to use the date formatter you need to set it like this and I cant figure out how to set another one without overwriting the current format.

DateFormatter.dateFormat = "MM-dd-yy hh:mm:ss a"

Solution

  • This is how to have multiple DateFormatter in a single View.

    Formatter1

    let formatter = DateFormatter()
     formatter.dateFormat = "QQQQ\ryyyy" //Shows Financial Quarter for Date
     let formattedDate = formatter.string(from: date)
    

    Formatter2

    let formatter1 = DateFormatter()
     formatter1.dateFormat = "EEEE, MMM d, yyyy" //Wednesday, Mar 29, 2017
     let formattedDate1 = formatter1.string(from: date)
    

    Refer to this site to know more about DateFormatter. Hope this helps. Happy Coding !