Convert date string swift

前端 未结 3 691
孤街浪徒
孤街浪徒 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 
    
    0 讨论(0)
  • 2020-12-09 05:50

    The standard ISO8601 date format with fractional seconds and time zone is

    yyyy-MM-dd'T'HH:mm:ss.SSSZ

    let myDate = "2016-06-20T13:01:46.457+02:00"
    let dateFormatter = NSDateFormatter()
    dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") // edited
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
    let date = dateFormatter.dateFromString(myDate)!
    dateFormatter.dateFormat = "dd/MM/yyyy"
    let dateString = dateFormatter.stringFromDate(date)
    

    Swift 3:

    let myDate = "2016-06-20T13:01:46.457+02:00"
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_US_POSIX") // edited
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
    let date = dateFormatter.date(from:myDate)!
    dateFormatter.dateFormat = "dd/MM/yyyy"
    let dateString = dateFormatter.string(from:date)
    

    In macOS 10.13+, iOS 11+ ISO8601DateFormatter is an alternative as input formatter

    let myDate = "2016-06-20T13:01:46.457+02:00"
    let inputFormatter = ISO8601DateFormatter()
    inputFormatter.formatOptions = [.withFullDate, .withFullTime, .withFractionalSeconds]
    let date = dateFormatter.date(from:myDate)!
    let outputFormatter = DateFormatter()
    outputFormatter.locale = Locale(identifier: "en_US_POSIX")
    outputFormatter.dateFormat = "dd/MM/yyyy"
    let dateString = dateFormatter.string(from:date)
    
    0 讨论(0)
  • 2020-12-09 05:58

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

        let myDate = datePicker.date
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
        let date = myDate
        dateFormatter.dateFormat = "dd/MM/yyyy"
        let dateString = dateFormatter.string(from:date)
        print(dateString)
    

    You can try manually also:

        let myDate = "2019-02-22T08:21:11+0000"
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
        let date = myDate
        dateFormatter.dateFormat = "dd/MM/yyyy"
        let dateString = dateFormatter.string(from:date)
        print(dateString)
    
    0 讨论(0)
提交回复
热议问题