這裏寫的是給一個日期計算出 這個日期所在的一週是幾號到幾號。吧日期換成今天日期也就是本週是幾號到幾號。考慮到有跨月,跨年等等的問題,直接就根據秒數來計算比較不會錯。在者考慮到過內外的一週是不同的。國內週一是一週的第一天也就是一週是週一到週日,國外是週日的一週的第一天也就是週日到下週的週六。ide
#import "ViewController.h" @interface ViewController () { UITextField * _textField; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 2016.8.8 星期一 // 2016.8.9 星期二 // 2016.8.11 星期4 // 2016.8.14 星期日 UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width-80)/2.0, 100, 80, 30)]; btn.backgroundColor = [UIColor orangeColor]; btn.tag = 1001; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn setTitle:@"國內計算" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; UIButton * btn2 = [[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width-80)/2.0, 140, 80, 30)]; btn2.backgroundColor = [UIColor orangeColor]; [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn2 setTitle:@"國外計算" forState:UIControlStateNormal]; btn2.tag = 1000; [btn2 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn2]; _textField = [[UITextField alloc]initWithFrame:CGRectMake((self.view.frame.size.width-150)/2.0, CGRectGetMinY(btn.frame)-50, 150, 30)]; _textField.backgroundColor = [UIColor yellowColor]; [self.view addSubview:_textField]; } - (void)btnAction:(UIButton*)sender { if (sender.tag == 1000) { NSDictionary * dic = [ViewController weekfirstAndLastDay:_textField.text Formater:@"yyyy.MM.dd" Foreign:YES]; NSLog(@"%@",dic); }else{ NSDictionary * dic = [ViewController weekfirstAndLastDay:_textField.text Formater:@"yyyy.MM.dd" Foreign:NO]; NSLog(@"%@",dic); } } +(NSDictionary *)weekfirstAndLastDay:(NSString*)dateStr Formater:(NSString*)formater Foreign:(BOOL)foreign { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:formater]; NSDate * date =[formatter dateFromString:dateStr]; NSArray * foreginDateArr = @[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"]; NSArray * chineseDateArr = @[@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六",@"星期日"]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"YYYY.MM.dd"]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *comp = [calendar components:kCFCalendarUnitYear|kCFCalendarUnitMonth|kCFCalendarUnitDay|kCFCalendarUnitWeekday|kCFCalendarUnitDay fromDate:date]; NSInteger foreginWeekDay = [comp weekday]; NSString * foreginDateStr = foreginDateArr[foreginWeekDay-1]; NSInteger chineseWeekDay = 0; if (foreign) { chineseWeekDay = foreginWeekDay; }else{ for (int i = 0; i < foreginDateArr.count; i++) { if ([foreginDateStr isEqualToString:chineseDateArr[i]]) { chineseWeekDay = i+1; break; } } } NSInteger gapDate = 7- chineseWeekDay; //把時間轉化成秒 NSTimeInterval dateStamp = [date timeIntervalSince1970]; //把秒轉化成時間 NSTimeInterval beginStamp = dateStamp - 24*60*60*(chineseWeekDay-1); NSTimeInterval endStamp = dateStamp + 24*60*60*gapDate; NSDate * begin = [NSDate dateWithTimeIntervalSince1970:beginStamp]; NSDate * end = [NSDate dateWithTimeIntervalSince1970:endStamp]; NSString * beginStr = [dateFormatter stringFromDate:begin]; NSString * endStr = [dateFormatter stringFromDate:end]; NSDictionary * dic = @{@"begin":beginStr, @"end":endStr, @"now":dateStr}; return dic; }
思路就是算出這個日期是屬於一週的第幾天,而後在相應的減去秒數,加上秒數算出一週是從幾號到幾號。code
分別計算了國內外component