iOS 技术分享

iOS - 获取当前日期的前几个月或后几个月

2020-04-07  本文已影响0人  Joh蜗牛

传入今日日期及距今的月份month:

- (NSDate*)getPriousorLaterDateFromDate:(NSDate*)date withMonth:(int)month{

    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMonth:month];
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];// NSGregorianCalendar
    NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
    return mDate;

}

eg:距今三个月前:

//获取当前时间,日期
    NSDate *currentDate = [NSDate date];
//获取前三个月的时间
    NSDate *monthagoData = [GlobelConfig getPriousorLaterDateFromDate:currentDate withMonth:-3];
上一篇 下一篇

猜你喜欢

热点阅读