Last active
February 20, 2023 14:41
-
-
Save dabear/14ce80d58f7e6c7633bdf5e5f86ef5fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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