Swift gets Current timestamp (sec, millisecond)

Source: Internet
Author: User
Swift gets Current timestamp (sec, millisecond)

It is often seen that a second-level timestamp is obtained in the form of an Int type direct * 1000来 MS, so that the final 3 digits are 0, and the results are not accurate enough. So there's another way to get a millisecond timestamp. Create a Date Extension with the following code

    Extension Date {

    ///gets the current second-level timestamp-10-bit
    var timestamp:string {let
        timeinterval:timeinterval = Self.timeinte rvalSince1970 let
        timeStamp = Int (timeinterval) return
        "\ (TimeStamp)"
    }

    ///Obtain the current millisecond timestamp-13-bit
    var millistamp:string {let
        timeinterval:timeinterval = self.timeintervalsince1970 let
        millisecond = CLongLong ( Round (timeinterval*1000)) return
        "\ (Millisecond)"
    }
}
Use
 Let millisecond = Date (). Millistamp
 Print (the current millisecond timestamp is millisecond = =, millisecond) let
 timeStamp = Date (). TimeStamp
 Print (the current second-level timestamp is timeStamp = =, TimeStamp)

/** 
    output
    The current millisecond timestamp is millisecond =  1524801851988
    The current second-level timestamp is TimeStamp = =  1524801851
* *
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.