skip to Main Content

I am converting the UTC date to a string. when the date convert to string successfully it is showing "2023-07-11 9:07:51U202fPM".dont know why its added unicode in the string.

func toString(formateType type: DateFormate) -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC") //TimeZone(abbreviation: "UTC")
    dateFormatter.dateFormat = type.rawValue
    return dateFormatter.string(from: self)
}

Date Format – "yyyy-MM-dd HH:mm:ss"
can you please help me how to fix this

2

Answers


  1. Check this code

    func toString(formatTpe: String) -> String {
            let dateFormatter = DateFormatter()
            dateFormatter.timeZone = TimeZone(abbreviation: "UTC") //TimeZone(abbreviation: "UTC")
            dateFormatter.dateFormat = formatTpe
            dateFormatter.locale = Locale(identifier: "en_us")
            return dateFormatter.string(from: Date())
        }
    

    //you need to call like this
    let date = toString(formatTpe: "yyyy-MM-dd HH:mm:ss")

    I have attach screenshot of result
    enter image description here

    Login or Signup to reply.
  2. let dateFormatter = DateFormatter()

    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"

    dateFormatter.timeZone = TimeZone(abbreviation: "UTC")

    let utcDate = Date()

    let utcString = dateFormatter.string(from: utcDate)

    print(utcString)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search