import Foundation
import TimeLapse
let currentDate = Date()
let fakePastDate = Calendar.current.date(byAdding: .second, value: -600, to: currentDate)! // 10 minutes ago
let timeLapse = fakePastDate.elapsedTime(until: currentDate)
print(timeLapse) // 10min
Date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy HH:mm:ss"
let fakeCurrentDate = dateFormatter.date(from: "24/12/2018 23:59:59")!
// some test dates
let date0 = dateFormatter.date(from: "24/12/2018 23:59:58")!
let date1 = dateFormatter.date(from: "24/12/2018 23:59:09")!
let date2 = dateFormatter.date(from: "24/12/2018 23:52:59")!
let date3 = dateFormatter.date(from: "24/12/2018 18:59:59")!
let date4 = dateFormatter.date(from: "23/12/2018 23:59:59")!
let date5 = dateFormatter.date(from: "09/12/2018 23:59:59")!
let date6 = dateFormatter.date(from: "24/11/2018 23:59:59")!
let date7 = dateFormatter.date(from: "24/12/2016 23:59:59")!
date0.elapsedTime(until: fakeCurrentDate)
date1.elapsedTime(until: fakeCurrentDate)
date2.elapsedTime(until: fakeCurrentDate)
date3.elapsedTime(until: fakeCurrentDate)
date4.elapsedTime(until: fakeCurrentDate)
date5.elapsedTime(until: fakeCurrentDate)
date6.elapsedTime(until: fakeCurrentDate)
date7.elapsedTime(until: fakeCurrentDate)
Conforms to the protocol TimeLapseFormat and assign custom format to TimeLapse.format
Example
struct CustomTimeLapseFormat: TimeLapseFormat {
var now : String = "now"
var second : String = "sec"
var minute : String = "m"
var hour : String = "hour"
var day : String = "d"
var week : String = "w"
var month : String = "mon"
var year : String = "y"
}
TimeLapse
Date
extension that compares anotherDate
with current time, converts it in seconds and returns a formatted time lapse description.Installation
Swift Package Manager
Example
Usage
Default
Date
Int (Seconds)
Customize Format
Conforms to the protocol
TimeLapseFormat
and assign custom format toTimeLapse.format
Example
Assign for usage:
Result:
Restore to default format
License
This program is free software; you can redistribute it and/or modify it under the terms of the MIT License.