获取日期格式字符串


- (NSString *)getWeek
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *now;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    now=[NSDate date];
    comps = [calendar components:unitFlags fromDate:[NSDate date]];
    NSInteger year=[comps year];
    int week = [comps weekday];
    int month = [comps month];
    int day = [comps day];
    int hour = [comps hour];
    int min = [comps minute];
    int sec = [comps second];
    
    NSArray * arrWeek=[NSArray arrayWithObjects:@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六", nil];
    NSString * weekStr = [NSString stringWithFormat:@"%@-%@-%@ %@",
                          [NSString stringWithFormat:@"%ld", (long)year],
                          [NSString stringWithFormat:@"%ld", (long)month],
                          [NSString stringWithFormat:@"%ld", (long)day],
                          [NSString stringWithFormat:@"%@",[arrWeek objectAtIndex:[comps weekday] - 1]]];
    
    return weekStr;
}

你可能感兴趣的:(获取日期格式字符串)