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)
}
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!
}
}