0

I have a String like "20170713 111326" and it should be changed to "13/07/2017" and "11:13 AM".What'll be the easiest way?

Win
  • 15
  • 3

1 Answers1

2

Swift 3.0

Try this code

let st = "20170713 111326"

let dateFmt = DateFormatter()
dateFmt.dateFormat = "yyyyMMdd HHmmss"

if let date = dateFmt.date(from: st) {

    dateFmt.dateFormat = "dd/MM/yyyy HH:mm a"
    let finalDate = dateFmt.string(from: date))
}
Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40
  • 1
    this date format it is wrong. HH is for 00-23 hours. For 12h clock am/pm time (1-12) he needs to use lowercase `h` so the time part should be `h:mm a` – Leo Dabus Jul 15 '17 at 03:33
  • @Jaydeep,your code is definitely what I'm looking for. Thank you for your help. Thanks – Win Jul 20 '17 at 10:20
  • @LeoDabus welcome, and if you found answer helpful you can accept answer so next time another user directly use it. – Jaydeep Vora Jul 20 '17 at 10:40