Getting the last day of a month

前端 未结 9 725
别跟我提以往
别跟我提以往 2020-12-08 00:24

How can I get the last day of the current month as an NSDate?

相关标签:
9条回答
  • 2020-12-08 00:43

    Version from iOS 7

    just to get rid of the deprecations... with a minor addition to get the last second of the day.

        // current date
        NSDate *now = [NSDate new];
    
        // get last day of the current month
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        [calendar setTimeZone:[NSTimeZone systemTimeZone]];
    
        NSRange dayRange = [ calendar rangeOfUnit:NSCalendarUnitDay
                                           inUnit:NSCalendarUnitMonth
                                          forDate:now];
    
        NSInteger numberOfDaysInCurrentMonth = dayRange.length;
    
        NSDateComponents *comp = [calendar components:
                                  NSCalendarUnitYear |
                                  NSCalendarUnitMonth |
                                  NSCalendarUnitDay fromDate:now];
    
        comp.day = numberOfDaysInCurrentMonth;
        comp.hour = 24;
        comp.minute = 0;
        comp.second = 0;
    
        NSDate *endOfMonth = [calendar dateFromComponents:comp];
    
    0 讨论(0)
  • 2020-12-08 00:45
    NSDate *curDate = [NSDate date];
            NSCalendar *currentCalendar = [NSCalendar currentCalendar];
            NSRange daysRange = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth
             forDate:curDate];
            NSDateComponents* comps = [currentCalendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:curDate];
            [comps setDay:daysRange.length];
            NSDate *maximumDate = [[NSCalendar currentCalendar] dateFromComponents:comps];
    
    0 讨论(0)
  • 2020-12-08 00:46

    Put this in a category:

    -(NSDate*)lastDayOfMonth
    {
        NSInteger dayCount = [self numberOfDaysInMonthCount];
    
        NSDateComponents *comp = [[self calendar] components:
                                  NSCalendarUnitYear |
                                  NSCalendarUnitMonth |
                                  NSCalendarUnitDay fromDate:self];
    
        [comp setDay:dayCount];
    
        return [[self calendar] dateFromComponents:comp];
    }
    
    -(NSInteger)numberOfDaysInMonthCount
    {
        NSRange dayRange = [[self calendar] rangeOfUnit:NSCalendarUnitDay
                                                 inUnit:NSCalendarUnitMonth
                                                forDate:self];
    
        return dayRange.length;
    }
    
    -(NSCalendar*)calendar
    {
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];    
        [calendar setTimeZone:[NSTimeZone timeZoneWithName:TIMEZONE]];
        return calendar;
    }
    
    0 讨论(0)
  • 2020-12-08 00:50
    // Adapted from running code in my app
    
    NSDate *curDate = [NSDate date];
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSRange daysRange = 
    [currentCalendar 
     rangeOfUnit:NSDayCalendarUnit 
     inUnit:NSMonthCalendarUnit 
     forDate:curDate];
    
    // daysRange.length will contain the number of the last day
    // of the month containing curDate
    
    NSLog(@"%i", daysRange.length);
    
    0 讨论(0)
  • 2020-12-08 00:50

    Swift version

    extension NSDate {
        func lastDayOfMonth() -> NSDate {
            let calendar = NSCalendar.currentCalendar()
            let dayRange = calendar.rangeOfUnit(.Day, inUnit: .Month, forDate: self)
            let dayCount = dayRange.length
            let comp = calendar.components([.Year, .Month, .Day], fromDate: self)
    
            comp.day = dayCount
    
            return calendar.dateFromComponents(comp)!
        }
    }
    
    0 讨论(0)
  • 2020-12-08 00:51

    With Swift 3 & iOS 10 the easiest way I found to do this is Calendar's dateInterval(of:for:):

    func monthInterval() -> DateInterval? {
        let calendar = Calendar.current
        guard let interval = calendar.dateInterval(of: .month, for: Date()) else { return nil }
        let duration = interval.duration
        // Subtract one second because dateInterval(of:for:)'s end date returns the beginning of the following month
        return DateInterval(start: interval.start, duration: duration - 1)
    }
    
    guard let interval = monthInterval() else { return }
    

    You can then use interval.start and interval.end to get the dates you need.

    0 讨论(0)
提交回复
热议问题