1

How can I compare the values from data Base to my current date ? A date is saved in the data base when some action is performed, I need to compare that date with my current date and do some action. How can I do that ?

The date is saved my date base in this format:

    var tasksin2 : Task?

    dateFormatter.dateFormat = "dd/MM/yyyy  hh:mm"

    let mydate="\(dateFormatter.string(from: datePicker.date))"

    tasksin2?.time=mydate

    (UIApplication.shared.delegate as! AppDelegate).saveContext()
S.Verma
  • 199
  • 2
  • 14
  • Do it the other way round: Make a `Date` from `tasksin2?.time`. However with your given code `tasksin2?.time` is `nil.` – vadian Apr 04 '17 at 15:19
  • @vadian Yes, I can do it by printing the saved date from data base to a label, but how do I compare it with my current time ? – S.Verma Apr 04 '17 at 15:20
  • Once again, to compare dates precisely you need `Date` instances. Comparing date string representations is cumbersome. – vadian Apr 04 '17 at 15:22
  • As @vadian stated, comparing two `Date` instances is easier and precise. Check this link http://stackoverflow.com/a/29319732/5997339 – ron27 Apr 04 '17 at 15:32

1 Answers1

7

Here ya go:

extension String {
    func toDate() -> Date? {
        let formatter = DateFormatter()
        formatter.dateFormat = "dd/MM/yyyy  hh:mm"
        return formatter.date(from: self)
    }
}

class MyViewController: UIViewController {

    var dateString1: String!
    var dateString2: String!

    override func viewDidLoad() {
        super.viewDidLoad()
        // set dateStrings
        printDateTypes()
    }

    func printDateTypes() {

        guard let date1 = dateString1.toDate() else { 
            print("dateString1: \(dateString1) | Failed to cast to \"dd/MM/yyyy  hh:mm\"")
            return 
        }

        guard let date2 = dateString2.toDate() else { 
            print("dateString2: \(dateString2) | Failed to cast to \"dd/MM/yyyy  hh:mm\"")
            return 
        }


        let isDescending = date1.compare(date2) == ComparisonResult.orderedDescending
        print("orderedDescending: \(isDescending)")

        let isAscending = date1.compare(date2) == ComparisonResult.orderedAscending
        print("orderedAscending: \(isAscending)")

        let isSame = date1.compare(date2) == ComparisonResult.orderedSame
        print("orderedSame: \(isSame)")
    }
}
Sethmr
  • 3,046
  • 1
  • 24
  • 42
  • Why the `if/else` in the `toDate` function? Just do `return formatter.date(from: self)`. – rmaddy Apr 04 '17 at 15:54
  • Because I was a less experience developer when I first wrote it. Good catch! rmaddy... It is starting to feel like you are stalking me >.< I appreciate your advice I suppose. – Sethmr Apr 04 '17 at 15:58
  • my date from the data base looks like `03/04/2017 10:54` and the current date I make looks like `5/4/2017 11:41` can these be compared ? – S.Verma Apr 05 '17 at 20:10
  • For one of them use "dd/MM/yyyy hh:mm" and the other use "d/MM/yyyy hh:mm"... If you need a good reference for the DateFormatter, use [this](http://www.codingexplorer.com/swiftly-getting-human-readable-date-nsdateformatter/). – Sethmr Apr 05 '17 at 20:16