Kevin Gobert
Kevin Gobert

Reputation: 85

NSDateFormatter to Int in Swift

I am trying to convert a NSDateFormater Int, but the variables return nil. This is the code I'm using:

    var datePeriodBegin:NSDate = NSDate()    
    if(NSUserDefaults.standardUserDefaults().objectForKey("datePeriodBegin") != nil){
        datePeriodBegin = (NSUserDefaults.standardUserDefaults().objectForKey("datePeriodBegin")! as? NSDate)!
    }

    let datePeriodEnd = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitMonth, value: 6, toDate: datePeriodBegin, options: nil)!


    var dateFormatter: NSDateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyyMMdd000000"

    var start:Int = dateFormatter.stringFromDate(datePeriodBegin).toInt()!
    var end:Int = dateFormatter.stringFromDate(datePeriodEnd).toInt()!

    println(start) // return nil in iPhone 4S -> iPhone 5s

Upvotes: 0

Views: 2517

Answers (2)

sketchyTech
sketchyTech

Reputation: 5906

Yene is absolutely correct, if you really need all those zeroes on the end then on a 32-bit device you need to convert to Int64 first:

  var datePeriodBegin:NSDate = NSDate()
        if(NSUserDefaults.standardUserDefaults().objectForKey("datePeriodBegin") != nil){
            datePeriodBegin = (NSUserDefaults.standardUserDefaults().objectForKey("datePeriodBegin")! as? NSDate)!
        }

        let datePeriodEnd = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitMonth, value: 6, toDate: datePeriodBegin, options: nil)!


        var dateFormatter: NSDateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyyMMdd"

        var start = dateFormatter.stringFromDate(datePeriodBegin).toInt()
        var end = dateFormatter.stringFromDate(datePeriodEnd).toInt()

        if let s = start {
            println(Int64(s)*1000000) // return 20150427000000 iPhone 4S -> iPhone 5
        }

On a 32-bit device the maximum value of an Int is the same as an Int32, which is 2147483647, whereas the maximum value of an Int64 is 9223372036854775807 (the same as an Int on a 64-bit device).

Update: iOS 9, Xcode 7

The code no longer works with current version of Xcode and Swift - here is the working code rewritten for iOS 9, Xcode 7:

var datePeriodBegin:NSDate = NSDate()
if(NSUserDefaults.standardUserDefaults().objectForKey("datePeriodBegin") != nil){
    datePeriodBegin = (NSUserDefaults.standardUserDefaults().objectForKey("datePeriodBegin")! as? NSDate)!
}

let datePeriodEnd = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.Month, value: 6, toDate: datePeriodBegin, options: [])!


var dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"

var start = Int(dateFormatter.stringFromDate(datePeriodBegin))
var end = Int(dateFormatter.stringFromDate(datePeriodEnd))

if let s = start {
    print(Int64(s)*1000000) // return 20150427000000 iPhone 4S -> iPhone 5
}

If it breaks in a future version of Xcode/Swift/iOS please comment and I'll update.

Upvotes: 0

Yannick
Yannick

Reputation: 545

Your date does not fit into Int32. iPhone 4s is 32 bit, iPhone 5s should be 64bit.

Upvotes: 3

Related Questions