1

I've got a string (eg. "2014-11-06 16:32:01") I want to turn this format into an NSDate object. Here's what I'm doing:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
var date = dateFormatter.dateFromString("2014-11-06 16:32:01")!
var myCalendar:NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!
var myComponents = myCalendar.components(NSCalendarUnit.WeekdayOrdinalCalendarUnit, fromDate: date)
var year = myComponents.year
var month = myComponents.month
var day = myComponents.day

year, month, and day are each valued at 9223372036854775807. Am I getting the dateFormat wrong?

Randall Stephens
  • 1,037
  • 1
  • 10
  • 16

2 Answers2

4

The date format is correct. But you have to specify all wanted units in the myCalendar.components(...) call. All other components are set to NSNotFound aka NSIntegerMax, which is 2^63 - 1 = 9223372036854775807 on the 64-bit architecture.

This should work:

var myComponents = myCalendar.components(.YearCalendarUnit | .MonthCalendarUnit | .DayCalendarUnit, fromDate: date)
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • This has given me the units: year = 4; month = -140590335715296; day = -36028797018959872; – Randall Stephens Nov 07 '14 at 23:00
  • @DaveAlton: That is strange, I had tested the code before posting it. What does `println(date)` show? How did you print year/month/day? – Martin R Nov 07 '14 at 23:01
  • For some reason println(date) isn't working. I'm getting the values by setting a breakpoint – Randall Stephens Nov 07 '14 at 23:09
  • Martin I just tested it. it showing correct date let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" var date = dateFormatter.dateFromString("2014-11-06 16:32:01")! var myCalendar:NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)! var myComponents = myCalendar.components(NSCalendarUnit.WeekdayOrdinalCalendarUnit, fromDate: date) println(date) var year = myComponents.year var month = myComponents.month var day = myComponents.day – Anton Nov 07 '14 at 23:10
  • @DaveAlton: Then you might be a "victim" of the "locale feature" (see http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformatter-locale-feature). Try adding `dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")` before the string-to-date conversion. – Martin R Nov 07 '14 at 23:15
  • @DaveAlton: Does `dateFormatter.dateFromString("2014-11-06 16:32:01")` (without the `!`) return `nil`? – Martin R Nov 07 '14 at 23:38
  • He needs only to deal with the 3 components he is trying to access. You can check how it can be done on my answer. – Leo Dabus Nov 10 '14 at 19:03
  • @LeonardoSavioDabus: Yes, and my suggested code shows how to fix OP's code to retrieve exactly the 3 components. I have tested that and it worked. – I really don't know why this answer deserves a downvote. – Martin R Nov 10 '14 at 19:16
  • @DaveAlton: Any progress on this problem? – Martin R Nov 28 '14 at 06:34
  • Hey, sorry. I gave up and worked on something else for a bit. Then my hard drive crashed a few weeks later. New hard drive and everything works fine now. Odd, eh? – Randall Stephens Nov 28 '14 at 18:55
1
static func convertStringToNSDate(dateStr: String) -> NSDate
{
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat =  "yyyy'-'MM'-'dd HH':'mm':'ss '+0000'"
    let date = dateFormatter.dateFromString(dateStr)

    return date!
}

On my code I am using that method in order to convert a string to NSDate. The only thing that you need to do is to change the dateFormat attribute to be the same as the date's format you want to convert.

Tzegenos
  • 837
  • 12
  • 16