Format a date from a string

前端 未结 5 1875
盖世英雄少女心
盖世英雄少女心 2020-12-10 08:37

I\'m trying to format a date from a string into another format.

For example: 2012-05-29 23:55:52 into 29/05 *newline* 2010.

I just

5条回答
  •  旧巷少年郎
    2020-12-10 08:58

    Please find following code to convert date from one format to another format. It will give time in your current zone.

    func convertDateFormat(sourceString : String, sourceFormat : String, destinationFormat : String) -> String{
    
        let dateFormatter = DateFormatter();
        dateFormatter.dateFormat = sourceFormat;
    
        if let date = dateFormatter.date(from: sourceString){
            dateFormatter.dateFormat = destinationFormat;
            return dateFormatter.string(from: date)
        }else{
            return ""
        }
    }
    

提交回复
热议问题