Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am updating some of my old Swift 2 answers to Swift 3. My answer to this question , though, is not easy to update since the question specifically asks for NSDate and not Date . So I am creating a new version of that question that I can update my answer for.

Question

If I start with a Date instance like this

let someDate = Date()

how would I convert that to an integer?

Related but different

These questions are asking different things:

  • Swift convert unix time to date and time
  • Converting Date Components (Integer) to String
  • Convert Date String to Int Swift
  • // convert Date to TimeInterval (typealias for Double) let timeInterval = someDate.timeIntervalSince1970 // convert to Integer let myInt = Int(timeInterval)

    Doing the Double to Int conversion causes the milliseconds to be lost. If you need the milliseconds then multiply by 1000 before converting to Int.

    Int to Date

    Including the reverse for completeness.

    // convert Int to TimeInterval (typealias for Double)
    let timeInterval = TimeInterval(myInt)
    // create NSDate from Double (NSTimeInterval)
    let myNSDate = Date(timeIntervalSince1970: timeInterval)
    I could have also used `timeIntervalSinceReferenceDate` instead of `timeIntervalSince1970` as long as I was consistent. This is assuming that the time interval is in seconds. Note that Java uses milliseconds.
    
  • For the old Swift 2 syntax with NSDate, see this answer.
  • Good point about Java, this is why you cannot really convert Date to Integer in Java, because it is simply not large enough to hold such values. In Java you'd need to use long for this. Refernce: stackoverflow.com/questions/12067697/… – lidkxx Jul 27, 2017 at 9:29

    If you are looking for timestamp with 10 Digit seconds since 1970 for API call then, below is code:

    Just 1 line code for Swift 4/ Swift 5

    let timeStamp = UInt64(Date().timeIntervalSince1970)

    print(timeStamp) <-- prints current time stamp

    1587473264

    let timeStamp = UInt64((Date().timeIntervalSince1970) * 1000) // will give 13 digit timestamp in milli seconds
                    Can use for API call, as they expect 10 digit time stamp above code should serve the purpose
    – vilas deshmukh
                    Apr 21, 2020 at 13:04
                    This is incorrect. Date().timeIntervalSince1970 will return a Double value in seconds. If you want it to be in milliseconds you need to multiply by 1000. Either way, you still need to convert it to a Int64: Int64(timeStamp * 1000).
    – mluisbrown
                    Oct 28, 2020 at 16:28
    

    timeIntervalSince1970 is a relevant start time, convenient and provided by Apple.

    If u want the int value to be smaller, u could choose the relevant start time you like

    extension Date{
        var intVal: Int?{
            if let d = Date.coordinate{
                 let inteval = Date().timeIntervalSince(d)
                 return Int(inteval)
            return nil
        // today's time is close to `2020-04-17 05:06:06`
        static let coordinate: Date? = {
            let dateFormatCoordinate = DateFormatter()
            dateFormatCoordinate.dateFormat = "yyyy-MM-dd HH:mm:ss"
            if let d = dateFormatCoordinate.date(from: "2020-04-17 05:06:06") {
                return d
            return nil
    extension Int{
        var dateVal: Date?{
            // convert Int to Double
            let interval = Double(self)
            if let d = Date.coordinate{
                return  Date(timeInterval: interval, since: d)
            return nil
    

    Use like this:

        let d = Date()
        print(d)
        // date to integer, you need to unwrap the optional
        print(d.intVal)
        // integer to date
        print(d.intVal?.dateVal)
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.