Suppose I have a simple String, extracted from an array of strings (representing dates) -
var myDateString = "2015-11-25 04:31:32.0"
Now, I want to convert it to a string that looks like Nov 25, 2015.
For that, I assume I need to -
- get a NSDate date object from string using NSDateFormatter
- get a string from that NSDate object by using some functionality (which I've been unable to find)
I've been trying this in Playground, but have been unable to solve such a simple problem.
Here is what I have tried -
Line 1| var dateFormatter = NSDateFormatter()
Line 2| dateFormatter.locale = NSLocale.currentLocale()
Line 3| dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss.A"
Line 4| let somedate = dateFormatter.dateFromString(str)
Line 5| let somedateString = dateFormatter.stringFromDate(somedate!)
In the sidebar of Xcode, line 4 prints "Nov 25, 2015, 12:00 AM", but when I try to print a string from somedate, I get "2015-11-25 00:00:00.0".
I haven't found a single proper explanation of NSDates anywhere.
This was so much easier in Java with parse() and format() functions for Dates.