21

I want two NSInteger year, month to go one month back. e.g. now is 2011-01 how can I get 2010-12. adding is one thing, but I want to subtract.

NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
[calendar release];
NSString * pdf_name = [NSString stringWithFormat:@"file_%d_%02d.pdf", year, month];

greets endo

EDIT: My solution

    NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
 NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

 NSDate *date = [NSDate date];
 NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
 NSInteger year = [dateComponents year];
 NSInteger month = [dateComponents month];

 BOOL substractOneMonth = YES;
 if (substractOneMonth) {
  if (month==1) {
   year--;
   month = 12;
  }else {
   month--;
  }
 }
 [calendar release];

 NSString * pdf_name = [NSString stringWithFormat:@"file_%d_%02d.pdf", year, month];
Cœur
  • 37,241
  • 25
  • 195
  • 267
endo.anaconda
  • 2,449
  • 4
  • 29
  • 55

4 Answers4

62

This sample code is from iPhone's Date and Time Programming Guide.

NSDate *today = [[NSDate alloc] init];
NSLog(@"%@", today);
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setMonth:-1]; // note that I'm setting it to -1
NSDate *endOfWorldWar3 = [gregorian dateByAddingComponents:offsetComponents toDate:today options:0];
NSLog(@"%@", endOfWorldWar3);

I hope this helps!

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
donkim
  • 13,119
  • 3
  • 42
  • 47
  • Sorry when im going with your solution im getting: 4021-12-12 08:32:40 +0000 – endo.anaconda Jan 06 '11 at 03:47
  • Huh, that's weird. Here's my output: 2011-01-06 03:49:28 +0000 and 2010-12-06 03:49:28 +0000. I'd love to hear an explanation why we're seeing different results.. – donkim Jan 06 '11 at 03:50
  • 1
    i see, sry i had old vars in use. shame on me! – endo.anaconda Jan 06 '11 at 03:59
  • Good to know! I, for one, was pretty surprised that the `NSDate` didn't handle more of this. Though I understand why they separated out this functionality, it seems more to Apple's pattern to make `NSDate` handle more of this type of stuff. – donkim Jan 06 '11 at 04:15
  • @donkim: Can you link to that programming guide, even if it's just an Amazon link? I searched and couldn't find anything by that name. Thanks. – Bill the Lizard Jan 06 '11 at 13:23
  • @endo: I made a very minor edit to the answer, which should allow you to change your vote. – Bill the Lizard Jan 06 '11 at 13:24
  • Thanks for the example....was very helpful! For those of you wanting to change the output format you can use NSDateFormatter and modify to your liking. – ninehundreds Apr 24 '12 at 21:21
12

I suggest creating an extension for this purpose:

extension Date {

    func adding(months: Int) -> Date? {
        let calendar = Calendar(identifier: .gregorian)

        var components = DateComponents()
        components.calendar = calendar
        components.timeZone = TimeZone(secondsFromGMT: 0)
        components.month = months

        return calendar.date(byAdding: components, to: self)
    }

}

Usage:

let now = Date()
let sixMonthsBefore = now.adding(months: -6)
let threeMonthsAfter = now.adding(months: 3)
Vadim Bulavin
  • 3,697
  • 25
  • 19
7

While the accepted answer works, it uses code depreciated in iOS 8.0. Another way to do the same thing without using deprecated code is as follows:

NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:-1];
date = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
1
NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];

month -= month - 1;

if (month < 1) {
 year -= 1;
 month = 12;
}

[calendar release];
NSString * pdf_name = [NSString stringWithFormat:@"file_%d_%02d.pdf", year, month];
Penang
  • 1,305
  • 13
  • 18