0

I want to work with to dates in this format: "dd/MM/yyyy"

The first is the actual date and the other is requested from Data Core, but when I want to compare if 2 dates are equals or not, I get the wrong format:

    let dateString = NSDate().dateToString(format:"dd/MM/yyyy")

    Params.insertValueWithKey(context: context, key: "simulationDate", value: dateString)

    let paramsDateString = Params.getValueFromKey(context: context, key: "simulationDate")

    let paramsDate = NSDate().stringToDate(dateString:paramsDateString, format:"dd/MM/yyyy")



    if NSDate() as Date != paramsDate {
        let actualDate = DateFormatter().string(from: NSDate() as Date)

        Params.insertValueWithKey(context: context, key: "simulationData", value: actualDate)
    }

enter image description here

The functions:

extension NSDate {
    func dateToString(format: String) -> String {
        var dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        var dateString = dateFormatter.string(from: self as Date)
        return dateString
    }

    func stringToDate(dateString: String, format: String?) -> Date{
        var dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        var date = dateFormatter.date(from: dateString)
        return date!
    }
}
Ben
  • 761
  • 1
  • 12
  • 35
  • I do not understand the actual issue? What is wrong? What is the actual and the expected behaviour? – shallowThought Jan 02 '17 at 15:40
  • @shallowThought I want to compare if a date is equal than an another, or do caculation like if a date subtract that the actual date is inferior than a week – Ben Jan 02 '17 at 15:52
  • See [this answer](http://stackoverflow.com/questions/39018335/swift-3-comparing-date-objects) – shallowThought Jan 02 '17 at 15:55

0 Answers0