标签:ios nsdatecomponents nsdateformatter 年月日 星期
#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) { weekStr=@"星期天"; }else if(week==2){ weekStr=@"星期一"; }else if(week==3){ weekStr=@"星期二"; }else if(week==4){ weekStr=@"星期三"; }else if(week==5){ weekStr=@"星期四"; }else if(week==6){ weekStr=@"星期五"; }else if(week==7){ weekStr=@"星期六"; } return weekStr; }
标签:ios nsdatecomponents nsdateformatter 年月日 星期
原文地址:http://blog.csdn.net/missafeng/article/details/45218951