-1

Could you please let me know How to convert the NSString of format (2015-04-30T21:53:11.0000) to NSDate? in Swift.

IOS Rocks
  • 2,127
  • 2
  • 21
  • 24
  • Read the documentation, perhaps? – Hot Licks Jul 05 '15 at 14:46
  • Whats does the capital T in the middle stand for? Is it Tuesday or Thursday? – applejuiceteaching Jul 05 '15 at 14:49
  • http://stackoverflow.com/questions/3917250/converting-nsstring-to-nsdate-and-back-again – Adrian Jul 05 '15 at 14:52
  • Here is Answer. func dateFromString(string: String) -> NSDate? { let df = NSDateFormatter() df.timeZone = NSTimeZone(abbreviation: "UTC") df.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'" var date: NSDate? = df.dateFromString(string) if date == nil { df.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'" date = df.dateFromString(string) } return date } – IOS Rocks Jul 06 '15 at 09:11

2 Answers2

2

Use the dateFromString method in iOS' date formatter class.

There are a zillion examples and explanations just a google search away. Most will be in Objective-C, but that's very easy to convert to Swift.

Read this.

meaning-matters
  • 21,929
  • 10
  • 82
  • 142
0

You would use NSDateFormatter for this.

let orig = "2015-04-30T21:53:11.0000"

let dsFormatter = NSDateFormatter()
dsFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSS"
let dsDate = dsFormatter.dateFromString(orig)
MirekE
  • 11,515
  • 5
  • 35
  • 28