我希望以这种格式与日期合作:" dd / MM / yyyy"
第一个是实际日期,另一个是从Data Core请求,但是当我想比较2个日期是否等于时,我的格式错误:
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)
}
功能:
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!
}
}