Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if 3 days have passed

Tags:

date

swift

How can I check to download some data every 3 days? All I need to do is check if 3 days or more have passed when the user opens the app, and if so, for the app to download some things? Here's what I have so far for comparing dates:

var shouldDownload: Bool = false

extension NSDate: Equatable {}
extension NSDate: Comparable {}

public func ==(lhs: NSDate, rhs: NSDate) -> Bool {
    return lhs.timeIntervalSince1970 == rhs.timeIntervalSince1970
}    

public func <(lhs: NSDate, rhs: NSDate) -> Bool {
    return lhs.timeIntervalSince1970 < rhs.timeIntervalSince1970
}

extension Int {
    var days: NSTimeInterval {
        let DAY_IN_SECONDS = 60 * 60 * 24
        var days:Double = Double(DAY_IN_SECONDS) * Double(self)
        return days
}
let lastChecked = NSDate()

let interval = NSDate(timeInterval: 3.days, sinceDate: lastChecked)
let today = NSDate()

if interval > today {
    shouldDownload = true
}
like image 492
brimstone Avatar asked May 07 '15 18:05

brimstone


3 Answers

You can use this simple function:

func isPassedMoreThan(days: Int, fromDate date : Date, toDate date2 : Date) -> Bool {
    let unitFlags: Set<Calendar.Component> = [.day]
    let deltaD = Calendar.current.dateComponents( unitFlags, from: date, to: date2).day
    return (deltaD ?? 0 > days)
}
like image 156
Max_Power89 Avatar answered Nov 15 '22 09:11

Max_Power89


Here's how I would do it. Every time your app launches or comes back to the foreground, do the following:

  • Read a saved NSDate from NSUserDefaults.

  • Figure out the number of midnights that have passed from the saved date until now. (Do a search in the Xcode docs on "Midnights" to find NSCalendar code for calculating the number of days between 2 dates.)

  • Save today's date to NSUserDefaults as the new saved date

I wrote this method as an extension to NSDate:

  /**
  This function calcuates an ordinal day number for the receiver 
  using the Gregorian calendar.

  :returns: an integer day number
  */

  func dayNumber() -> Int
  {
    let calendar = DateUtils.gregorianCalendar
    return calendar.ordinalityOfUnit(NSCalendarUnit.CalendarUnitDay,
      inUnit: NSCalendarUnit.CalendarUnitEra, forDate: self)
  }

It uses a class DateUtils to manage a single instance of the Gregorian NSCalendar. That lets me avoid creating a new instance of the Gregorian calendar for each date operation I need to do.

class DateUtils
{
  static let gregorianCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
}
like image 41
Duncan C Avatar answered Nov 15 '22 09:11

Duncan C


Swift 4 version:

func isPassedMoreThan(days: Int, fromDate date : Date, toDate date2 : Date) -> Bool {
    let unitFlags: Set<Calendar.Component> = [.day]
    let deltaD = Calendar.current.dateComponents( unitFlags, from: date, to: date2)
    return deltaD > days
}
like image 31
valvoline Avatar answered Nov 15 '22 09:11

valvoline