0

is there a way to get the date in the format of "May 17, 2019" in swift, currently I have

var date = Date(timeIntervalSinceReferenceDate: (Date().timeIntervalSinceReferenceDate))
var stringDate = "\(date)"
stringDate = String(stringDate.prefix(16))

and it has the format of "2019-17-05 1:31" for example

Rob
  • 415,655
  • 72
  • 787
  • 1,044
ShedSports
  • 541
  • 2
  • 7
  • 14
  • 1
    Use [`DateFormatter`](https://developer.apple.com/documentation/foundation/dateformatter) and set the `dateStyle` (not the `dateFormat`). – Rob May 17 '19 at 19:09
  • 2
    Your first line should be: `let date = Date()`. – rmaddy May 17 '19 at 19:11
  • 1
    Surely you can find your answer in [these search results](https://stackoverflow.com/search?q=%5Bswift%5D+how+to+format+date). – rmaddy May 17 '19 at 19:13

1 Answers1

-1
let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MMMM d, yyyy"
        var date = Date(timeIntervalSinceReferenceDate: (Date().timeIntervalSinceReferenceDate))
        print(dateFormatter.string(from: date))
ShedSports
  • 541
  • 2
  • 7
  • 14