iOS development-swift time stamp conversion method

Source: Internet
Author: User
Swift implements simple timestamp conversion

The publication timestamp is compared to the current time: a few seconds ago ... Days ago. Swift Syntax Time Stamp Transformation code block Createdtime: Publish time stamp

Swift Timestamp Conversion method func timestamp ()-> string {var timestamp = string () var now = time_t () time (&now)
    Createdtime publish timestamp var distance = Int (Difftime (now, createdtime)) if distance < 0 {Distance = 0 } if distance < timestamp = nsstring (format: "%d%@", distance, (distance = 1)? "Seconds ago": "Seconds Ago") as String} else if distance < distance = Distance/60 timestamp = Stri NG (format: "%d%@", distance, (distance = 1)? "Minutes ago": "Minutes Ago")} else if distance < * distance = distance/60/60 timestamp = St Ring (format: "%d%@", distance, (distance = 1)? "Hours ago": "Hours Ago")} else if distance < * * 7 {distance = distance/60/60/24 times Tamp = String (format: "%d%@", distance, (distance = = 1)?
        "Days before": "Days Before")} else if distance < * 7 * 4 {distance = DISTANCE/60/60/24/7; timestamp = String (format: "%d%@", distance, (distance = 1)? "Weeks ago": "Weeks Ago")} else {Let dateformatter = NSDateFormatter () If Dateformatter = "" {L ET dateformatter = NSDateFormatter () Dateformatter.timestyle =. Shortstyle Dateformatter.datestyle =. Shortstyle} Let date = NSDate (timeintervalsince1970:createdtime) timestamp = Dateformatter.strin
 Gfromdate (date)} return timestamp}
you are welcome to study and progress together ^. ^^. ^
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.