话不多说,上代码
// // EBDate.h // ChargeMoney // // Created by eagle on 15-3-24. // Copyright (c) 2015年 EagleB. All rights reserved. // #import <Foundation/Foundation.h> @interface EBDate : NSObject /** * 得到这个周的第一天和最后一天 */ +(NSArray *)getFirstAndLastDayOfThisWeek; /** * 得到这个月的第一天和最后一天 */ +(NSArray *)getFirstAndLastDayOfThisMonth; /** * 得到今天的第一天和最后一天 */ +(NSArray *)getFirstAndLastDayOfThisYear; @end
// // EBDate.m // ChargeMoney // // Created by eagle on 15-3-24. // Copyright (c) 2015年 EagleB. All rights reserved. // #import "EBDate.h" @implementation EBDate +(NSArray *)getFirstAndLastDayOfThisWeek { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]]; NSInteger weekday = [dateComponents weekday]; //第几天(从sunday开始) NSInteger firstDiff,lastDiff; if (weekday == 1) { firstDiff = -6; lastDiff = 0; }else { firstDiff = - weekday + 2; lastDiff = 8 - weekday; } NSInteger day = [dateComponents day]; NSDateComponents *firstComponents = [calendar components:NSWeekdayCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]]; [firstComponents setDay:day+firstDiff]; NSDate *firstDay = [calendar dateFromComponents:firstComponents]; NSDateComponents *lastComponents = [calendar components:NSWeekdayCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]]; [lastComponents setDay:day+lastDiff]; NSDate *lastDay = [calendar dateFromComponents:lastComponents]; return [NSArray arrayWithObjects:firstDay,lastDay, nil]; } +(NSArray *)getFirstAndLastDayOfThisMonth { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDate *firstDay; [calendar rangeOfUnit:NSMonthCalendarUnit startDate:&firstDay interval:nil forDate:[NSDate date]]; NSDateComponents *lastDateComponents = [calendar components:NSMonthCalendarUnit | NSYearCalendarUnit |NSDayCalendarUnit fromDate:firstDay]; NSUInteger dayNumberOfMonth = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]].length; NSInteger day = [lastDateComponents day]; [lastDateComponents setDay:day+dayNumberOfMonth-1]; NSDate *lastDay = [calendar dateFromComponents:lastDateComponents]; return [NSArray arrayWithObjects:firstDay,lastDay, nil]; } +(NSArray *)getFirstAndLastDayOfThisYear { //通过2月天数的改变,来确定全年天数 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setTimeZone:[NSTimeZone systemTimeZone]]; [formatter setDateFormat:@"yyyy"]; NSString *dateStr = [formatter stringFromDate:[NSDate date]]; dateStr = [dateStr stringByAppendingString:@"-02-14"]; [formatter setDateFormat:@"yyyy-MM-dd"]; NSDate *aDayOfFebruary = [formatter dateFromString:dateStr]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDate *firstDay; [calendar rangeOfUnit:NSYearCalendarUnit startDate:&firstDay interval:nil forDate:[NSDate date]]; NSDateComponents *lastDateComponents = [calendar components:NSMonthCalendarUnit | NSYearCalendarUnit | NSDayCalendarUnit fromDate:firstDay]; NSUInteger dayNumberOfFebruary = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:aDayOfFebruary].length; NSInteger day = [lastDateComponents day]; [lastDateComponents setDay:day+337+dayNumberOfFebruary-1]; NSDate *lastDay = [calendar dateFromComponents:lastDateComponents]; return [NSArray arrayWithObjects:firstDay,lastDay, nil]; } @end
感谢这篇文章:http://my.oschina.net/yongbin45/blog/156181