-
Re: Swift 3 Check Two times against each other help
QuinceyMorris Jan 24, 2017 1:11 AM (in response to JayCo)You have to be more explicit about what you want — "time" is too ambiguous.
A Date value is actually a date/time. If you want to compare the current date/time with the date/time at which something was last accessed, you should use the Date values, and not try to convert to strings or Ints. (Note: You can store a Date value in user defaults.) Such Date values are independent of time zone, daylight savings time, etc.
If you mean a time of day, for example if you want to make a new backup of a file every day at a certain time, you're going to have to work a bit harder to deal with time zones, etc.
-
Re: Swift 3 Check Two times against each other help
eskimo Jan 24, 2017 3:11 AM (in response to QuinceyMorris)If you mean a time of day, for example if you want to make a new backup of a file every day at a certain time, you're going to have to work a bit harder to deal with time zones, etc.
Which we covered over in this thread.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardwarelet myEmail = "eskimo" + "1" + "@apple.com"
-
Re: Swift 3 Check Two times against each other help
JayCo Jan 24, 2017 4:26 AM (in response to eskimo)Hey,
I thought out the time process again and came to a much simpler way. I'm getting the current time from Date so this should solve any time issues. If the device travels back in time then I will catch it by < 1 hour period.
Thanks
-
-
Re: Swift 3 Check Two times against each other help
JayCo Jan 24, 2017 4:24 AM (in response to QuinceyMorris)Hey,
Essentially I wish to minimise an API call to 1 hour intervals. If an hour has passed then allow the API call.
-
Re: Swift 3 Check Two times against each other help
QuinceyMorris Jan 24, 2017 10:11 AM (in response to JayCo)So let's try a simpler version of your function:
func checkTimer() -> Bool { let time = Date () print(time) if let storedTime = UserDefaults.standard.object(forKey: "LastAccessTime")) as? Date, time >= storedTime { print("Enough time has passed \(storedTime)") return true } print("Not enough time has passed \(storedTime)") return false }
Note that I've used an "if let" construct to check if the last access time exists in user defaults, and a Swift 3 "if XXX, YYY {…}" construct to test for both conditions XXX (last access time exists) and YYY (it's in the past) simultaneously.
Is this the functionality you're looking for?
-
Re: Swift 3 Check Two times against each other help
goldsdad Jan 24, 2017 11:47 AM (in response to JayCo)Maybe the Timer class will help - https://developer.apple.com/reference/foundation/timer
-
-