NSDate subtract one month

隐身守侯 提交于 2019-11-29 11:50:38

问题


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];

回答1:


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!




回答2:


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];



回答3:


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)



回答4:


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];


来源:https://stackoverflow.com/questions/4611390/nsdate-subtract-one-month

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!