Xcode swift am/pm time to 24 hour format

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

    Here is the answer with more extra format.

    ** Xcode 12, Swift 5.3 **

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "HH:mm:ss"
    var dateFromStr = dateFormatter.date(from: "12:16:45")!
    
    dateFormatter.dateFormat = "hh:mm:ss a 'on' MMMM dd, yyyy"
    //Output: 12:16:45 PM on January 01, 2000
    
    dateFormatter.dateFormat = "E, d MMM yyyy HH:mm:ss Z"
    //Output: Sat, 1 Jan 2000 12:16:45 +0600
    
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
    //Output: 2000-01-01T12:16:45+0600
    
    dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
    //Output: Saturday, Jan 1, 2000
    
    dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
    //Output: 01-01-2000 12:16
    
    dateFormatter.dateFormat = "MMM d, h:mm a"
    //Output: Jan 1, 12:16 PM
    
    dateFormatter.dateFormat = "HH:mm:ss.SSS"
    //Output: 12:16:45.000
    
    dateFormatter.dateFormat = "MMM d, yyyy"
    //Output: Jan 1, 2000
    
    dateFormatter.dateFormat = "MM/dd/yyyy"
    //Output: 01/01/2000
    
    dateFormatter.dateFormat = "hh:mm:ss a"
    //Output: 12:16:45 PM
    
    dateFormatter.dateFormat = "MMMM yyyy"
    //Output: January 2000
    
    dateFormatter.dateFormat = "dd.MM.yy"
    //Output: 01.01.00
    
    //Output: Customisable AP/PM symbols
    dateFormatter.amSymbol = "am"
    dateFormatter.pmSymbol = "Pm"
    dateFormatter.dateFormat = "a"
    //Output: Pm
    
    // Usage
    var timeFromDate = dateFormatter.string(from: dateFromStr)
    print(timeFromDate)
    

提交回复
热议问题