iOS NSDate 转换成星期

//日期转化成星期几

-(NSString*)getWeek:(NSString *)date

{

    NSDateFormatter * weekDateFormatter =[[NSDateFormatter alloc]init];

    [weekDateFormatter setDateFormat:@"yyyy-MM-dd"];

    [weekDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    NSDate * weekData =[weekDateFormatter dateFromString:date];

    NSLog(@"--------%@",weekData);

    NSCalendar * calendar =[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents * comps =[[NSDateComponents alloc]init];

    

    NSInteger unitFlags = NSYearCalendarUnit| NSMonthCalendarUnit| NSDayCalendarUnit| NSWeekdayCalendarUnit |NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

    

    calendar.locale =[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];

    

    comps  =[calendar components:unitFlags fromDate:weekData];

    NSArray * arr =@[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];

    

    return [NSString stringWithFormat:@"%ld日-%@",(long)comps.day ,arr[comps.weekday-1]];

}


你可能感兴趣的:(iOS源码,UI)