swift日期计算:比如 2020-07-27 加 30天 后的 日期为:2020-08-26;若是 2020-07-27 的月份直接加一个月,则日期为:2020-08-27。
时间加减需要基于 SwiftDate 库来实现;用pods导入 swiftDate;
pod 'SwiftDate'
不会使用 cocoapods 的赶紧去学下哈!(估计现在还不会使用cocoapods的IOS程序员应该已经很少见了吧)
正题:
导入成功后,在使用的地方引用 import SwiftDate;
//现在的时间
let nowDate = Date()
//在现在时间的基础上,加了一年,一月,一日
let date1 = nowDate + 1.years + 1.months + 1.days
//在现在时间的基础上减了一年,二月,三日,四小时
let date2 = nowDate - 1.years - 2.months - 3.days - 4.hours


参考自:https://blog.csdn.net/weixin_44836266/article/details/94399054
网友评论