定义全局变量,firstDayOfWeek是开始的日期,lastDayOfWeek是结束的日期:
NSDate *firstDayOfWeek;
NSDate *lastDayOfWeek;
NSCalendar *calendar;
NSDate *date;
我这边获取的是当前日期:
- (void)getNowTime{
date = [NSDate date];
calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
year = [comps year];
month = [comps month];
day = [comps day];
week = [comps weekday];
}
也可以改成其它的日期:
[comps setMonth:6];
[comps setDay:1];
[comps setYear:2017];
NSDate *weekDate = [calendar dateFromComponents:comps];
int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *weekComps = [calendar components:unitFlags fromDate:weekDate];
// [weekComps weekday] 当前日期是周几
// [weekComps day] 得到几号
[self getWeekDate:weekComps Week:[weekComps weekday] Day:[weekComps day]];
最后直接调用一下方法:
- (void)getWeekDate:(NSDateComponents*)comps1 Week:(NSInteger)weekDay Day:(NSInteger)getDay{
// 计算当前日期和这周的星期一和星期天差的天数
long firstDiff,lastDiff;
if (weekDay == 1){
firstDiff = -6;
lastDiff = 0;
}else{
firstDiff = [calendar firstWeekday] - weekDay + 1;
lastDiff = 8 - weekDay;
}
[comps1 setDay:getDay+firstDiff];
firstDayOfWeek = [calendar dateFromComponents:comps1];
[comps1 setDay:getDay+lastDiff];
lastDayOfWeek = [calendar dateFromComponents:comps1];
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"MM.dd"];
//起始日期
NSString *str = [NSString stringWithFormat:@"%@-%@",[formater stringFromDate:firstDayOfWeek],[formater stringFromDate:lastDayOfWeek]];
}
最近写的这个项目的需求是获取一直到当前日期的月日日期、所有周的起始日期和所有的年月,就写了一下:
#import <UIKit/UIKit.h>
@interface BasicViewController : UIViewController
{
NSMutableArray *MD_DateArray;
NSMutableArray *Z_DateArray;
NSMutableArray *YM_DateArray;
}
@end
#import "BasicViewController.h"
#define beginYear 2017 //开始日期
@interface BasicViewController ()
{
NSCalendar *calendar;
NSDate *date;
NSInteger year;
NSInteger month;
NSInteger day;
NSInteger week;
NSDate *firstDayOfWeek;
NSDate *lastDayOfWeek;
NSString *lastDate;
}
@end
@implementation BasicViewController
- (void)viewDidLoad {
[super viewDidLoad];
MD_DateArray = [NSMutableArray array];
Z_DateArray = [NSMutableArray array];
YM_DateArray = [NSMutableArray array];
[self reloadData];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[UIBarButtonItem appearance]setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
}
- (void)reloadData{
[self getNowTime];
}
- (void)getNowTime{
date = [NSDate date];
calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
year = [comps year];
month = [comps month];
day = [comps day];
week = [comps weekday];
[self getAllMonth:comps];
[self getAllYear:comps];
}
- (void)getAllMonth:(NSDateComponents*)comps{
for (int i = 1; i<month+1+(year-beginYear)*12; i++) {
if (i%12 != 0) {
[comps setMonth:i%12];
}
// [comps setMonth:i];
[comps setDay:1];
[comps setYear:beginYear+i/12];
date = [calendar dateFromComponents:comps];
NSLog(@"date:%@",date);
NSInteger number = (NSInteger)[[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date].length;//一个月的天数
NSLog(@"====================i:%d,number:%ld",i,(long)number);
if ((beginYear+i/12 == year) && (i == month)) {
number = day;
}
if (i%12 == 0) {
i = 12;
}
NSLog(@"day:%ld",(long)day);
for (int j = 1; j<number+1; j++) {
[MD_DateArray addObject:[NSString stringWithFormat:@"%d.%d",i%12,j]];
[comps setMonth:i];
[comps setDay:j];
[comps setYear:beginYear+i/12];
NSDate *weekDate = [calendar dateFromComponents:comps];
int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *weekComps = [calendar components:unitFlags fromDate:weekDate];
[self getWeekDate:weekComps Week:[weekComps weekday] Day:[weekComps day]];
}
}
}
- (void)getAllYear:(NSDateComponents*)comps{
int mon = 0;
int yea = 0;
for (int i = 1; i<month+1+(year-beginYear)*12; i++) {
mon = i;
if (i%12 == 0) {
mon = 12;
}else{
mon = i%12;
}
if (i<13) {
yea = 0;
}else{
yea = i/12;
}
[YM_DateArray addObject:[NSString stringWithFormat:@"%d年%d月",beginYear+yea,mon]];
}
}
- (void)getWeekDate:(NSDateComponents*)comps1 Week:(NSInteger)weekDay Day:(NSInteger)getDay{
// 计算当前日期和这周的星期一和星期天差的天数
long firstDiff,lastDiff;
if (weekDay == 1){
firstDiff = -6;
lastDiff = 0;
}else{
firstDiff = [calendar firstWeekday] - weekDay + 1;
lastDiff = 8 - weekDay;
}
[comps1 setDay:getDay+firstDiff];
firstDayOfWeek = [calendar dateFromComponents:comps1];
[comps1 setDay:getDay+lastDiff];
lastDayOfWeek = [calendar dateFromComponents:comps1];
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"MM.dd"];
NSString *str = [NSString stringWithFormat:@"%@-%@",[formater stringFromDate:firstDayOfWeek],[formater stringFromDate:lastDayOfWeek]];
if ([str isEqualToString:lastDate]) {
return;
}else{
[Z_DateArray addObject:[NSString stringWithFormat:@"%@-%@",[formater stringFromDate:firstDayOfWeek],[formater stringFromDate:lastDayOfWeek]]];
lastDate = str;
}
// NSLog(@"Z_DateArray:%@",Z_DateArray);
// }
}
@end
因为有多个类要使用,所以我直接写在父类里边,我感觉我写的比较麻烦,谁有更简单方法的,希望赐教。