I am trying to convert the current time to an Int (without the AM/PM addition) and doing calculations with it. For instance, compare it to an older time and calculate the difference, then show the difference in hours:minutes. But I am really struggling here.
Getting the time:
func getTime() -> String {
let formatter = DateFormatter()
formatter.timeStyle = .long
let dateString = formatter.string(from: Date())
return dateString
}
I'd like to remove the AM/PM here.
Saving time to UserDefaults:
UserDefaults.standard.set(self.currentTime, forKey: "startTime")
Getting the saved time and current time and do some calculations here:
let startTime = UserDefaults.standard.integer(forKey: "startTime")
let currentTime = getTime()
let timeDiff = currentTime - startTime
Obviously this doesn't work because it's all strings. I know how to convert a string of integers to int: Int(getTime()) ?? 0 for instance, but as long as I cannot remove the AM/PM this all doesn't work of course.
What can I do or am I making this more difficult than it should be?