获得一个月的最后一天

时间:2010-05-05 10:28:06

标签: objective-c cocoa-touch cocoa date nsdate

如何将当月的最后一天作为NSDate

9 个答案:

答案 0 :(得分:71)

NSDate *curDate = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; // Get necessary date components

// set last of month
[comps setMonth:[comps month]+1];
[comps setDay:0];
NSDate *tDateMonth = [calendar dateFromComponents:comps];
NSLog(@"%@", tDateMonth);

也应该有用。

答案 1 :(得分:41)

// 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);

答案 2 :(得分:9)

把它放在一个类别中:

-(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;
}

答案 3 :(得分:3)

Swift版本

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)!
    }
}

答案 4 :(得分:0)

iOS 7版本

只是为了摆脱弃用......只需要一点点补充即可获得当天的最后一秒。

constructor

答案 5 :(得分:0)

Swift 3.0.1版本

extension Date {
func lastDayOfMonth() -> Date {
    let calendar = NSCalendar.current
    let dayRange = calendar.range(of: .day, in: .month, for: self)
    let dayCount = dayRange?.count
    var comp = calendar.dateComponents([.year, .month, .day], from: self)
    comp.day = dayCount
    return calendar.date(from: comp)!
    }
}

答案 6 :(得分:0)

使用Swift 3& iOS 10我发现这是最简单的方法Calendar 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 }

然后,您可以使用interval.startinterval.end来获取所需的日期。

答案 7 :(得分:0)

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

答案 8 :(得分:-2)

解决方法功能:

-(int)GetLastDayOfMonth:(NSDate *)date
{
    int last_day = 27;
    NSCalendar *cal=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *compsMonth = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date];
    [compsMonth setDay:last_day];
    int month = [compsMonth month]; 
    while(TRUE){
        [compsMonth setDay:last_day+1];
        NSDate *dateFuture = [cal dateFromComponents:compsMonth];
        NSDateComponents *futureComps = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:dateFuture];
        if(month != [futureComps month]){
            return last_day;
        }
        last_day+=1;
    }
    return last_day;
}