objective-c - 如何为 NSDate 添加 1 天?

标签 objective-c swift date cocoa-touch nsdate

基本上,正如标题所说。我想知道如何将 1 天添加到 NSDate

如果是这样的话:

21st February 2011

它会变成:

22nd February 2011

如果是的话:

31st December 2011

它会变成:

1st January 2012.

最佳答案

Swift 5.0:

var dayComponent    = DateComponents()
dayComponent.day    = 1 // For removing one day (yesterday): -1
let theCalendar     = Calendar.current
let nextDate        = theCalendar.date(byAdding: dayComponent, to: Date())
print("nextDate : \(nextDate)")

Objective-C :

NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = 1;

NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *nextDate = [theCalendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0];

NSLog(@"nextDate: %@ ...", nextDate);

这应该是不言自明的。

关于objective-c - 如何为 NSDate 添加 1 天?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5067785/

相关文章:

iPhone/iPad 用户文件跟踪

iphone - 我如何计算从我现在所在的位置到特定点的距离

java - 如何获取这一天(例如 : Monday) from a time Instant in FreeMarker

php - 即使使用日期对象,日期时间比较也无法在 PHP 中按预期工作

objective-c - 这是在 iOS8 中获取文档目录路径的最佳方式

ios - 带有 @tags 的 UITextView 和视觉效果背后的隐藏信息

iOS - 使用动态 key 解码嵌套 JSON

swift - Firebase Xcode Swift - 卡在 downloadURL 上

iOS:有没有一种方法可以根据单击的表格 View 单元格使用条件 segue

Linux 日期演算返回 2 个月前的奇数结果