-2

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

Rob
  • 415,655
  • 72
  • 787
  • 1,044
karthi
  • 31
  • 1
  • 8
  • Your sample string has seven millisecond characters (which is odd, you usually see 0, 3 or 6 digits for milliseconds), but then the example in your text only has three characters. You also say you want it in UTC, but then showed us a date that is in some other timezone. Which is it? – Rob Dec 24 '18 at 14:24
  • Also, I’d suggest setting the `locale` of the formatter to `Locale(identifier: ”en_US_POSIX”)`, assuming, of course, that you want to save this string in some invariant format for storing this string and/or sending it to some web service. See [Technical Note TN1480](https://developer.apple.com/library/archive/qa/qa1480/_index.html). – Rob Dec 24 '18 at 14:29

1 Answers1

3

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

Sagar Chauhan
  • 5,715
  • 2
  • 22
  • 56