Skip to content

Instantly share code, notes, and snippets.

@dabear
Last active February 20, 2023 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dabear/14ce80d58f7e6c7633bdf5e5f86ef5fd to your computer and use it in GitHub Desktop.
Save dabear/14ce80d58f7e6c7633bdf5e5f86ef5fd to your computer and use it in GitHub Desktop.
import Foundation
extension Date{
// Calculates the progress made from the start of the given range to the current date
// Returns a percentage value between 0 and 100
func getProgress(range: ClosedRange<Date>) -> Double {
if (self >= range.upperBound ) {
return 100
} else if (self <= range.lowerBound) {
return 0
}
let totalTime = range.upperBound.timeIntervalSince(start)
let elapsed = self.timeIntervalSince(start)
// Calculate the progress made so far as a percentage of the total time
return (elapsed / totalTime) * 100
}
}
let start = Date() - 100000
let now = Date()
let end = Date() + 2500000
print("Start: \(start)\nEnd: \(end)\nNow: \(now)")
let progress = now.getProgress(range: start...end)
print("progress: \(progress)%")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment