本文介绍了如何为 NSDate 添加 1 天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Basically, as the title says. I'm wondering how I could add 1 day to an NSDate.
So if it were:
21st February 2011It would become:
22nd February 2011Or if it were:
31st December 2011It would become:
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);This should be self-explanatory.
更多推荐
如何为 NSDate 添加 1 天?
发布评论