Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking when a date has passed - Swift

Well, the title pretty much says it all. What I am trying to do is check when a date has passed. So, for example let us say that a user is using my app and then they go to bed and check my app in the morning. When my app opens up I need to check if the day has changed at all.

Also I don't really need to know this information when the app is terminated or in the background or anything. I just need to know if the date has changed when the app is running and the user is actually interacting with it.

Note: I have looked at other Stack Overflow Posts pertaining to this issue but none of them have helped me.

like image 693
Harish Avatar asked Feb 20 '16 16:02

Harish


People also ask

How do you check if a date is greater than today in Swift?

Show activity on this post. if dateIsBefore("12/25/2016") { print("Not Yet Christmas 2016 :(") } else { print("Christmas Or Later!") } if dateIsBefore("12/25/2016", referenceDate:"12/31/2016") { print("Christmas comes before new years!") } else { print("Something is really wrong with the world...") }

How do I compare two dates in Swift?

let date1 = Date() let date2 = Date(). addingTimeInterval(100) if date1 == date2 { ... } else if date1 > date2 { ... } else if date1 < date2 { ... } if i want to ignore time. e.g. 2019-05-14 12:08:14 +0000 == 2019-05-14 should return true.

What is timestamp in Swift?

A timestamp is that which contain some characters that are in an encoded form which will contain any event, date or time etc. For more information, you can also go through from here.


Video Answer


2 Answers

Implement to observe

NSCalendarDayChangedNotification

Posted whenever the calendar day of the system changes, as determined by the system calendar, locale, and time zone. This notification does not provide an object.

If the the device is asleep when the day changes, this notification will be posted on wakeup. Only one notification will be posted on wakeup if the device has been asleep for multiple days.

There are no guarantees about the timeliness of when this notification will be received by observers. As such, you should not rely on this notification being posted or received at any precise time.

The notification is posted through [NSNotificationCenter defaultCenter].

Example:

In applicationDidFinishLaunching add

NSNotificationCenter.defaultCenter().addObserver(self, selector:"calendarDayDidChange:", name:NSCalendarDayChangedNotification, object:nil)

and implement the method

func calendarDayDidChange(notification : NSNotification)
{
   doSomethingWhenDayHasChanged()
}

or use the block API.

If the class including the observer is not the application delegate class you might remove the observer at some time.

like image 58
vadian Avatar answered Nov 09 '22 10:11

vadian


Update vadian's reply to Swift 5:

NotificationCenter.default.addObserver(self, selector:#selector(self.calendarDayDidChange(_:)), name:NSNotification.Name.NSCalendarDayChanged, object:nil)

and implement the method

@objc private func calendarDayDidChange(_ notification : NSNotification) {
    doSomethingWhenDayHasChanged()
}
like image 39
villb Avatar answered Nov 09 '22 10:11

villb