分类中写
static func getLastMonth(num : Int) -> String {
let curDate = Date()
let formater = DateFormatter()
formater.dateFormat = "MM"
let calendar = Calendar(identifier: .gregorian)
var lastMonthComps = DateComponents()
// year = 1表示1年后的时间 year = -1为1年前的日期,month day 类推
lastMonthComps.month = num
let newDate = calendar.date(byAdding: lastMonthComps, to: curDate)
let dateStr = formater.string(from: newDate!)
return dateStr
}
网友评论