简体   繁体   中英

Convert dates to multiple formats with SwiftDate

Is it possible to convert a date, say it is in minutes to another format using SwiftDate library? For example:

77 minutes = 1 hour, 17 minutes

Why not to use this library? You can import NSDateTimeAgo.bundle and NSDate+Extension.swift . And to convert your timestamp to lets say 1 hour, 17 minutes you can do like this:

let timestamp = //timestamp or time
let date = Date(timeIntervalSince1970: timestamp!)
let timeAgo = date.timeAgo
print(timeAgo)

I am suggesting you this because it is more lightweight. And if you do not need the "ago" then you can remove those from NSDateTimeAgo.string file.

But you can also do it without any library like this eg with seconds:

func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) {
    return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
}

func secondsToHoursMinutesSeconds (seconds:Int) -> () {
    let (h, m, s) = secondsToHoursMinutesSeconds (seconds: seconds)
    print ("\(h) Hours, \(m) Minutes, \(s) Seconds")
}
secondsToHoursMinutesSeconds(seconds: 3000)// it would print 0 Hours, 50 Minutes, 0 Seconds

You can also suit it for your needs(this code is from this answer).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM