#pragma mark - 获取年月日及星期 + (NSString *)stringFromDate { // 添加日期 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *comps = [[NSDateComponents alloc] init]; NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDate * senddate=[NSDate date]; NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; [dateformatter setDateFormat:@"YYYY-MM-dd"]; comps = [calendar components:unitFlags fromDate:senddate]; NSString *locationString=[dateformatter stringFromDate:senddate]; NSString *date_weekStr = [NSString stringWithFormat:@"%@ %@",locationString,[self week:[comps weekday]]]; return date_weekStr; } #pragma mark - 推算星期 +(NSString *)week:(NSInteger)week { NSString *weekStr=nil; if(week==1) { [email protected]"星期天"; }else if(week==2){ [email protected]"星期一"; }else if(week==3){ [email protected]"星期二"; }else if(week==4){ [email protected]"星期三"; }else if(week==5){ [email protected]"星期四"; }else if(week==6){ [email protected]"星期五"; }else if(week==7){ [email protected]"星期六"; } return weekStr; }
时间: 2024-10-30 02:43:13