Get time with AM and PM from date iOS swift

Problem: 

I had a date with me and i want to get the time only from the date.How to achieve that?

Solution:

extension Date {

    func getTime() -> String {

        let format = DateFormatter()

        format.timeZone = NSTimeZone.local

        format.dateFormat = "hh:mm a"

        return format.string(from: date)

    }

}


Call the above function as like below


myDate.getTime()

Comments

Popular posts from this blog

store cgpoint in userdefaults iOS swift

libc++abi: terminating due to uncaught exception of type NSException iOS swift