NullHypothesis
NullHypothesis

Reputation: 4516

Timestamp (milliseconds) in Swift

I am receiving a creation date for an object in a database as milliseconds (# of milliseconds since epoch or whatever) and would like to convert it to/from a string in Swift!

I think I'd need a data type of CUnsignedLong? I am trying something like this but it outputs the wrong number:

    var trial: CUnsignedLong = 1397016000000
    println(trial)  //outputs 1151628800 instead!

I'm guess this is the wrong data type so what would you all advise in a situation like this? In Java I was using long which worked.

Thanks!

Upvotes: 2

Views: 6811

Answers (2)

Softlabsindia
Softlabsindia

Reputation: 921

 func currentTimeMillis() -> Int64{
    let nowDouble = NSDate().timeIntervalSince1970
    return Int64(nowDouble*1000)
}

Working fine

Upvotes: 6

Martin R
Martin R

Reputation: 539945

On 32-bit platforms, CUnsignedLong is a 32-bit integer, which is not large enough to hold the number 1397016000000. (This is different from Java, where long is generally a 64-bit integer.)

You can use UInt64 or NSTimeInterval (a type alias for Double), which is what the NSDate methods use.

Upvotes: 4

Related Questions