从当前date减去7天

看来我不能从当前date减去7天。 这是我如何做到这一点:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; [offsetComponents setDay:-7]; NSDate *sevenDaysAgo = [gregorian dateByAddingComponents:offsetComponents toDate:[NSDate date] options:0]; 

SevenDaysAgo获得与当前date相同的值。

请帮忙。

编辑:在我的代码,我忘记了取代获取当前date的variables与正确的。 所以上面的代码是function。

使用dateByAddingTimeInterval方法:

 NSDate *now = [NSDate date]; NSDate *sevenDaysAgo = [now dateByAddingTimeInterval:-7*24*60*60]; NSLog(@"7 days ago: %@", sevenDaysAgo); 

输出:

 7 days ago: 2012-04-11 11:35:38 +0000 

希望能帮助到你

码:

 NSDate *currentDate = [NSDate date]; NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; [dateComponents setDay:-7]; NSDate *sevenDaysAgo = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:currentDate options:0]; NSLog(@"\ncurrentDate: %@\nseven days ago: %@", currentDate, sevenDaysAgo); [dateComponents release]; 

输出:

 currentDate: 2012-04-22 12:53:45 +0000 seven days ago: 2012-04-15 12:53:45 +0000 

我完全同意JeremyP。

BR。
尤金

如果你至less运行的是iOS 8或OS X 10.9,那么还有更简洁的方法:

 NSDate *sevenDaysAgo = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:-7 toDate:[NSDate date] options:0]; 

或者,用Swift 2:

 let sevenDaysAgo = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: -7, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0)) 

随着Swift 3,它变得更加紧凑:

 let sevenDaysAgo = Calendar.current.date(byAdding: .day, value: -7, to: Date()) 

Swift 3

 Calendar.current.date(byAdding: .day, value: -7, to: Date()) 

dymv的答案很好。 这可以在迅速使用

 extension NSDate { static func changeDaysBy(days : Int) -> NSDate { let currentDate = NSDate() let dateComponents = NSDateComponents() dateComponents.day = days return NSCalendar.currentCalendar().dateByAddingComponents(dateComponents, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))! } } 

你可以调用这个

 NSDate.changeDaysBy(-7) // Date week earlier NSDate.changeDaysBy(14) // Date in next two weeks 

希望它有助于和thx dymv

适用于SWIFT 3.0

这里是fucntion,你可以通过任何计数例如在这里减less日,月,日,我已经减less了100年的当前系统date年,你可以做它的日,月也只是设置柜台,并存储在一个数组,你可以在这个数组的任何地方func currentTime()

  { let date = Date() let calendar = Calendar.current var year = calendar.component(.year, from: date) let month = calendar.component(.month, from: date) let day = calendar.component(.day, from: date) let pastyear = year - 100 var someInts = [Int]() printLog(msg: "\(day):\(month):\(year)" ) for _ in pastyear...year { year -= 1 print("\(year) ") someInts.append(year) } print(someInts) } 

Swift 3.0

 let date = Date() - 7 * 24 * 60 * 60 

Swift 3:

对Dov答案的修改。

 extension Date { func dateBeforeOrAfterFromToday(numberOfDays :Int?) -> Date { let resultDate = Calendar.current.date(byAdding: .day, value: numberOfDays!, to: Date())! return resultDate } } 

用法:

 let dateBefore = Date().dateBeforeOrAfterFromToday(numberOfDays : -7) let dateAfter = Date().dateBeforeOrAfterFromToday(numberOfDays : 7) print ("dateBefore : \(dateBefore), dateAfter :\(dateAfter)")