当前日期转成大写格式

项目中用到了当前日期以大写显示的需求,各种百度,找不到,于是就自己写了一个
方便其他人查阅
+(NSString *)dateUpperChange
{
    NSArray *upper = @[@"零",@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"十"];
    //根据小写数字格式的日期转换成大写格式的日期
    NSDateFormatter* formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *time = [formatter stringFromDate:[NSDate date]];
    if (time.length == 0) {
        return @"";
    }
    time = [time stringByReplacingOccurrencesOfString:@"-" withString:@""];
    if (time.length != 8) {
        return @"";
    }
    NSMutableString *mString = [[NSMutableString alloc]init];
    for (int i = 0; i < 4; i ++) {//年
        NSString *str = [time substringWithRange:NSMakeRange(i, 1)];
        NSInteger intStr = [str integerValue];
        [mString appendString:upper[intStr]];
    }
    //    [mString appendString:@"年"];//拼接年
    //月
    NSInteger month = [[time substringWithRange:NSMakeRange(4, 2)] integerValue];
    if (month <= 10) {
        [mString appendString:upper[month]];
    }else{
        [mString appendString:@"十"];
        [mString appendString:upper[month % 10]];
    }
    [mString appendString:@"月"];
    
    //日
    NSInteger day = [[time substringWithRange:NSMakeRange(6, 2)] integerValue];
    if (day <= 10) {
        [mString appendString:upper[day]];
    }else if (day < 20){
        [mString appendString:@"十"];
        [mString appendString:upper[day % 10]];
    }else{
        [mString appendString:upper[day / 10]];
        [mString appendString:@"十"];
        NSInteger tmp = day % 10;
        if (tmp != 0) {
            [mString appendString:upper[tmp]];
        }
    }
    [mString appendString:@"号"];
    
    return mString;
}

备注:"地球历" 三个字是我自己加上去的,上面的方法只返回当前大写日期

当前日期转成大写格式_第1张图片
效果图

你可能感兴趣的:(当前日期转成大写格式)