2

I'm trying to get the difference in two Date objects. I know how to do it basically, which is like this:

        let date = Date()
        let dateFormatter = DateFormatter()
        dateFormatter.locale = Locale.current

        print(count)
        // print(history[count - 1].value(forKey: "multipleURLs") as! [String])
        let latestDate = history[count - 1].value(forKey: "date") as! Date
        let calendar = Calendar.current

        let components = calendar.dateComponents([.year, .month, .day, .hour], from: date)

        let nowDate = calendar.startOfDay(for: date)
        let lastDate = calendar.startOfDay(for: latestDate)

        let diffDateComponent = (calendar as NSCalendar).components([NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.day], from: lastDate, to: nowDate, options: NSCalendar.Options.init(rawValue: 0))

        print("Status Checked" + String(describing: diffDateComponent.day))

But I'm trying to get the result as a day start from 6 a.m. in the morning instead of 0.

So I did something like this:

        let nowDate = calendar.startOfDay(for: date.addingTimeInterval(-3600))
        let lastDate = calendar.startOfDay(for: latestDate.addingTimeInterval(-3600))

But it doesn't seem to be working, can anyone help me with this?

JoshJoshJosh
  • 897
  • 2
  • 11
  • 20

2 Answers2

0

This will set each date to 6 AM (in the current locale of the Date):

import Foundation

// set up dates
let calendar = Calendar.current
let date = Date()
if let latestDate = calendar.date(byAdding: .day, value: -5, to: date) {

  // set each day to 6 AM
  if let nowDate  = calendar.date(bySettingHour: 6, minute: 0, second: 0, of: date      ),
     let lastDate = calendar.date(bySettingHour: 6, minute: 0, second: 0, of: latestDate) {

    print(    date.description(with: .current),
           nowDate.description(with: .current),
          lastDate.description(with: .current),
          separator: "\n")

    // calculate difference in days
    if let days = calendar.dateComponents([.day], from: lastDate, to: nowDate).day {
      print("Days since: \(days)")
    }
  }
}

// Thursday, June 8, 2017 at 7:59:42 AM Eastern Daylight Time
// Thursday, June 8, 2017 at 6:00:00 AM Eastern Daylight Time
// Saturday, June 3, 2017 at 6:00:00 AM Eastern Daylight Time
// Days since: 5
0

Thanks guy, I finally figured it out. In case anyone need it, this is how it's done:

        let date = Date()
        let dateFormatter = DateFormatter()
        dateFormatter.locale = Locale.current

        let latestDate = history[count - 1].value(forKey: "date") as! Date
        let calendar = Calendar.current

        let nowDate = calendar.date(byAdding: .hour, value: -6, to: date)
        let lastDate = calendar.date(byAdding: .hour, value: -6, to: latestDate)

        let finalNowDate = calendar.startOfDay(for: nowDate!)
        let finalLastDate = calendar.startOfDay(for: lastDate!)

        let diffDateComponent = calendar.dateComponents([.day], from: finalLastDate, to: finalNowDate)
        print("dif\(diffDateComponent.day)")
JoshJoshJosh
  • 897
  • 2
  • 11
  • 20
  • Why are you moving back the time 6 hours, then getting the start of day? You might move back to a previous day if the date is before 6 AM. Perhaps your question doesn’t reflect what you’re actually trying to accomplish because I don’t see how this does what you’re asking. Also, avoid forcing your `Optional` values with `!`. It may work most times but you’re asking for runtime crashes down the line. Test and unwrap them safely. –  Jun 10 '17 at 18:03