Xcode swift am/pm time to 24 hour format

后端 未结 11 1061
甜味超标
甜味超标 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:27

    Swift version 3.0.2 , Xcode Version 8.2.1 (8C1002) (12 hr format ):

    func getTodayString() -> String{
    
            let formatter = DateFormatter()
            formatter.dateFormat = "h:mm:ss a "
            formatter.amSymbol = "AM"
            formatter.pmSymbol = "PM"
    
            let currentDateStr = formatter.string(from: Date())
            print(currentDateStr)
            return currentDateStr 
    }
    

    OUTPUT : 12:41:42 AM

    Feel free to comment. Thanks

提交回复
热议问题