Search code examples
iosswiftnsdateformatter

Convert current date into yyyy-MM-ddTHH:mm:ss.fffffffzzz format


Code:

let format1 = DateFormatter()
format1.dateFormat = "yyyy-MM-ddTHH:mm:ss.fffffffzzz" 
format1.timeZone = TimeZone(abbreviation: "UTC")
print(format1.string(from:Date()))

I am trying to get date string in this format(2018-12-19T12:30:00.000 +11:00) but i got date(2018-12-12) only.what is wrong with mycode.any help will be appricated.thanks in advance


Solution

  • You are not using proper format, please try this will help you.

    let format1 = DateFormatter()
    format1.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS ZZZ"
    format1.timeZone = TimeZone(abbreviation: "UTC")
    format1.locale = Locale(identifier: "en_US_POSIX")
    print(format1.string(from:Date()))
    

    This will print following output:

    2018-12-24T14:16:11.011 +0000