Xcode swift am/pm time to 24 hour format

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

    Unfortunately apple priority the device date format, so in some cases against what you put, swift change your format to 12hrs

    To fix this is necessary to use setLocalizedDateFormatFromTemplate instead of dateFormat an hide the AM and PM

    let formatter = DateFormatter()
    formatter.setLocalizedDateFormatFromTemplate("HH:mm:ss a")
    formatter.amSymbol = ""
    formatter.pmSymbol = ""
    formatter.timeZone = TimeZone(secondsFromGMT: 0)
    var prettyDate = formatter.string(from: Date())
    

    You can check a very useful post with more information detailed in https://prograils.com/posts/the-curious-case-of-the-24-hour-time-format-in-swift

提交回复
热议问题