//获取本周七天日期
-(NSMutableArray *)latelyEightTime{
NSMutableArray *weekArr = [[NSMutableArray alloc] init];
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:nowDate];
// 获取今天是周几
NSInteger weekDay = [comp weekday];
// 获取几天是几号
NSInteger day = [comp day];
// 计算当前日期和本周的星期一和星期天相差天数
long first,last;
if (weekDay == 1){
first = -6;
last = 0;
}
else{
first = [calendar firstWeekday] - weekDay + 1;
last = 8 - weekDay;
}
NSDateComponents *baseDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:nowDate];
//获取周一日期
[baseDayComp setDay:day + first];
NSDate *firstDayOfWeek = [calendar dateFromComponents:baseDayComp];
//计算从周一开始的七天日期
for (int i = 0; i < 7; i ++) {
//从现在开始的24小时
NSTimeInterval secondsPerDay = i * 24*60*60;
NSDate *curDate = [NSDate dateWithTimeInterval:secondsPerDay sinceDate:firstDayOfWeek];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"M.d"];
NSString *dateStr = [dateFormatter stringFromDate:curDate];//几月几号
NSDateFormatter *weekFormatter = [[NSDateFormatter alloc] init];
[weekFormatter setDateFormat:@"EEEE"];//星期几 @"HH:mm 'on' EEEE MMMM d"];
NSString *weekStr = [weekFormatter stringFromDate:dateStr];
NSString *chinaStr = [self theWeek:weekStr];
NSString *strTime = [NSString stringWithFormat:@"%@%@",dateStr,chinaStr];
NSLog(@"%@",strTime);
[weekArr addObject:strTime];
}
return weekArr;
}
//转换英文为中文
-(NSString *)theWeek:(NSString *)theWeek{
NSString *Str;
if(theWeek){
if([theWeek isEqualToString:@"Monday"]){
Str = @"星期一";
}else if([theWeek isEqualToString:@"Tuesday"]){
Str = @"星期二";
}else if([theWeek isEqualToString:@"Wednesday"]){
Str = @"星期三";
}else if([theWeek isEqualToString:@"Thursday"]){
Str = @"星期四";
}else if([theWeek isEqualToString:@"Friday"]){
Str = @"星期五";
}else if([theWeek isEqualToString:@"Saturday"]){
Str = @"星期六";
}else if([theWeek isEqualToString:@"Sunday"]){
Str = @"星期日";
}
}
return Str;
}
iOS获取周一到周天七天日期和对应的星期几
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...