0

I have a string representing a date

"2016-06-28T22:26:31.200577Z"

and the dateformatter I use uses the format

"yyyy-MM-dd'T'HH:mm:ssZ"

the extra ".200577" is causing my dateformatter to return nil when I try convert the date string to a NSDate object.

I have tried changing my dateformatter string to

"yyyy-MM-dd'T'HH:mm:ss.ssssssZ"

but this is not correct. I am unsure whether the ".200577" actually denotes a fraction of a second or is something else like a time zone.. At the moment I can get past this error by splitting the end part off of the string but I do not want to lose this information.

I am looking for help as to what my correct dateformatter string should be.

1
  • Did the code work, its working at my end Commented Jul 5, 2016 at 7:04

2 Answers 2

3

You can follow the Unicode Markup Language its

yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"

Reference : SO Question

Sign up to request clarification or add additional context in comments.

Comments

1

Use the following code to get formatted NSDate object

let dateformatter = NSDateFormatter()
 dateformatter.dateStyle = NSDateFormatterStyle.MediumStyle
 dateformatter.timeStyle = NSDateFormatterStyle.NoStyle
 dateformatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"
 print(dateformatter.dateFromString("2016-06-28T22:26:31.200577Z"))

// Optional(2016-06-28 22:26:31 +0000)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.