常规获取当前时间等操作就不在赘述。这里分享下自己写的关于得到当前年、月、周的第一天和最后一天的方法。
在.h文件里声明方法
@interface ZYModel : NSObject
+(ZYModel *)getInstance;
/** * 得到这个月的第一天和最后一天 */
- (NSArray*)getFirstAndLastDayOfMonth:(NSInteger)month inYear:(NSInteger)year;
/** * 得到今年的第一天和最后一天 */
- (NSArray*)getFirstAndLastDayOfYear:(NSInteger)year;
@end
在.m文件里实现
static ZYModel* hudInstance = nil;
@interface ZYModel()
@property (nonatomic,strong) NSArray * jiduArr;
@end
@implementation ZYModel
+(ZYModel *)getInstance{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
hudInstance = [[ZYModel alloc] init];
});
return hudInstance;
}
-(NSArray*)getFirstAndLastDayOfMonth:(NSInteger)month inYear:(NSInteger)year {
NSArray * dateArr = [NSArray new];
dateArr = [self getjidumonth:month];
NSString * str1 = dateArr[0];
NSString * str2 = dateArr[2];
NSInteger firstteger = [str1 integerValue];
NSInteger lastteger = [str2 integerValue];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date = [self getDateWithMonth:firstteger year:year];
NSDate *datelast = [self getDateWithMonth:lastteger year:year];
NSDate *firstDay;
NSTimeInterval interval;
[calendar rangeOfUnit:NSCalendarUnitMonth startDate:&firstDay interval:&interval forDate:date];
NSDate *last_Day;
NSTimeInterval interval_last;
[calendar rangeOfUnit:NSCalendarUnitMonth startDate:&last_Day interval:&interval_last forDate:datelast];
NSDateComponents *lastDateComponents = [calendar components:NSCalendarUnitMonth | NSCalendarUnitYear |NSCalendarUnitDay fromDate:last_Day];
NSUInteger dayNumberOfMonth = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:datelast].length;
NSInteger day = [lastDateComponents day];
[lastDateComponents setDay:day + dayNumberOfMonth];
lastDateComponents.nanosecond = lastDateComponents.nanosecond - 1000000;
NSDate *lastDay = [calendar dateFromComponents:lastDateComponents];
return [NSArray arrayWithObjects:firstDay,lastDay, nil];
}
-(NSArray*) getjidumonth:(NSInteger)month{
NSArray * dateArr = [NSArray new];
NSString * str = [NSString stringWithFormat:@"%ld",month];
for (int i = 0; i NSArray * arr = self.jiduArr[i]; for (int j= 0; j if ([str isEqualToString:arr[j]]) { dateArr = arr; } } } return dateArr; } - (NSDate *)getDateWithMonth:(NSInteger)month year:(NSInteger)year { NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; dateComponents.year = year; dateComponents.month = month; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDate *date = [calendar dateFromComponents:dateComponents]; return date; } + (NSArray*)getFirstAndLastDayOfYear:(NSInteger)year { NSDate *date = [self getDateWithYear:year]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDate *firstDay; [calendar rangeOfUnit:NSCalendarUnitYear startDate:&firstDay interval:nil forDate:date]; NSDateComponents *lastDateComponents = [calendar components:NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitDay fromDate:firstDay]; NSUInteger dayNumberOfYear = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitYear forDate:date].length; NSInteger day = [lastDateComponents day]; [lastDateComponents setDay:day + dayNumberOfYear]; lastDateComponents.nanosecond = lastDateComponents.nanosecond - 1000000; NSDate *lastDay = [calendar dateFromComponents:lastDateComponents]; return [NSArray arrayWithObjects:firstDay,lastDay, nil]; } + (NSDate *)getDateWithYear:(NSInteger)year { NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; dateComponents.year = year; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDate *date = [calendar dateFromComponents:dateComponents]; return date; } -(NSArray*)jiduArr{ if (_jiduArr == nil) { _jiduArr = @[@[@"1",@"2",@"3"], @[@"4",@"5",@"6"], @[@"7",@"8",@"9"], @[@"10",@"11",@"12"] ]; } return _jiduArr; } @end 在要获取操作的地方 进行处理 eg:获取一个季度的开始日期和结束日期 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSArray* dateArray = [NSArray new]; dateArray = [[ZYModel getInstance] getFirstAndLastDayOfMonth:10 inYear:2017]; NSDate * date1 = [dateArray objectAtIndex:0]; NSDate * date2 = [dateArray objectAtIndex:1]; NSString * StrDate1 = [self getdateStr:date1]; NSString * StrDate2 = [self getdateStr:date2]; NSLog(@"dat1==%@ date2==%@",StrDate1,StrDate2); } -(NSString*)getdateStr:(NSDate*)date{ NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器 dateFormatter.dateFormat=@"yyyy-MM-dd ";//指定转date得日期格式化形式 return [dateFormatter stringFromDate:date];//2015-11-20 08:24:04 }