Convert date string swift

前端 未结 3 690
孤街浪徒
孤街浪徒 2020-12-09 05:42

I have a date string in this format:

2016-06-20T13:01:46.457+02:00

and I need to change it to something like this:

20/06/20         


        
3条回答
  •  时光说笑
    2020-12-09 05:45

    I always use this code while converting String to Date . (Swift 3)

    extension String
    {
         func  toDate( dateFormat format  : String) -> Date
        {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = format
            if let date = dateFormatter.date(from: self)
            {
                return date
            }
            print("Invalid arguments ! Returning Current Date . ")
            return Date()
        }
    }
    

    and just call like . .

    print ( "2016-06-20T13:01:46.457+02:00".toDate(dateFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ") )
    //Capital HH for 24-Hour Time 
    

提交回复
热议问题