ios - 距另一个日期一年的日期

标签 ios swift swift2

我正在尝试计算距当前日期一年后的日期:

let calendar = NSCalendar.currentCalendar()
let currDate = NSDate()
let newDate = calendar.dateBySettingUnit(.Year, value: 2017, ofDate: currDate, options: NSCalendarOptions(rawValue: 0))!

但是 newDate 始终是 2017 年 1 月 1 日。

最佳答案

你差不多明白了

let calendar = NSCalendar.currentCalendar()
let currDate = NSDate()
let newDate = calendar.dateByAddingUnit(.Year, value: 1, toDate: currDate, options: [])

关于ios - 距另一个日期一年的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35118907/

相关文章:

ios - 当部分没有行时确定 UITableView 的可见部分

ios - 从应用程序委托(delegate)访问导航 Controller

ios - 无法为 Xcode 4.5 中的 Single ViewController 类加载两个 Nib (.XIB)

swift - 在 UINavigationController 下输入时 UISearchController 会向上移动

ios - 使用 ReactiveCocoa 4 的简单 UIGestureRecogniser 示例

ios - UIViewRepresentable.updateUIView(_ :context:) is not called when @State, @Binding 或 @EnvironmentObject 已更改

ios - 是否可以在 swift 3 的 tableview 单元格中实现 tableview?

ios - Swift 慢速 collectionView

ios - 谷歌在 swift 中放置自动完成功能并不完美

swift - 如何过滤在 Realm swift 中为当前日期创建的事件?