Xcode swift am/pm time to 24 hour format

后端 未结 11 1082
甜味超标
甜味超标 2020-11-28 04:40

I am trying to convert a am/pm format time to a 24 hour format time

6:35 PM to 18:35

I tried this piece of code on playground but it doesnt

11条回答
  •  眼角桃花
    2020-11-28 05:19

    Use this function for date conversion , its working fine when your device in 24/12 hr formate

    func convertDateFormater(fromFormate:String,toFormate:String,_ dateString: String) -> String{
        let formatter = DateFormatter()
        formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale
        formatter.dateFormat = fromFormate
        let date = formatter.date(from: dateString)
        formatter.dateFormat = toFormate
        return  date != nil ? formatter.string(from: date!) : ""
    }
    

提交回复
热议问题