最近项目遇到一个需要把日期转换成中文的需求,做完了记录一下。
有需要的可以节省时间拿去用哦。
1.工具类.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface QSUtilClass : NSObject
/**
今天时间转换 格式 二零一九年七月二日
@return 格式 二零一九年七月二日
*/
+ (NSString *)dateFormattertranceformWithToday;
/**
时间转换 格式 二零一九年七月二日
@param date NSDate
@return 格式 二零一九年七月二日
*/
+ (NSString *)dateFormattertranceformWithDate:(NSDate *)date;
@end
NS_ASSUME_NONNULL_END
2.工具类.m
#import "QSUtilClass.h"
static NSDateFormatter * dateFormatter;
@implementation QSUtilClass
+ (NSString *)dateFormattertranceformWithToday {
return [self.class dateFormattertranceformWithDate:[NSDate date]];
}
+ (NSString *)dateFormattertranceformWithDate:(NSDate *)date {
if (!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
}
// 年
[dateFormatter setDateFormat:@"YYYY"];
NSString *yearStr = [dateFormatter stringFromDate:date];
yearStr = [self.class yearTransform:yearStr];
// 月
[dateFormatter setDateFormat:@"M"];
NSString *monStr = [dateFormatter stringFromDate:date];
monStr = [self.class monthTransform:monStr];
// 日
[dateFormatter setDateFormat:@"d"];
NSString *dateStr = [dateFormatter stringFromDate:date];
dateStr = [self.class dayTransform:dateStr];
NSString * resultString = [NSString stringWithFormat:@"%@年%@月%@日",yearStr,monStr,dateStr];
NSLog(@"resultString:%@",resultString);
return resultString;
}
+ (NSString *)yearTransform:(NSString *)year {
NSArray *arabicNumeralsArray = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
NSArray *chineseNumeralsArray = @[@"零",@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:chineseNumeralsArray forKeys:arabicNumeralsArray];
NSString * resultString = [NSString stringWithFormat:@"%@%@%@%@",[dictionary objectForKey:[year substringWithRange:NSMakeRange(0, 1)]],[dictionary objectForKey:[year substringWithRange:NSMakeRange(1, 1)]],[dictionary objectForKey:[year substringWithRange:NSMakeRange(2, 1)]],[dictionary objectForKey:[year substringWithRange:NSMakeRange(3, 1)]]];
return resultString;
}
+ (NSString *)monthTransform:(NSString *)month {
NSString *arabicNumStr = month;
NSArray *arabicNumeralsArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"];
NSArray *chineseNumeralsArray = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"十",@"十一",@"十二"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:chineseNumeralsArray forKeys:arabicNumeralsArray];
NSString *chinese = [dictionary objectForKey:arabicNumStr];
return chinese;
}
+ (NSString *)dayTransform:(NSString *)day {
NSArray *arabicNumeralsArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
NSArray *chineseNumeralsArray = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:chineseNumeralsArray forKeys:arabicNumeralsArray];
if (day.length == 1) {
return [dictionary objectForKey:day];
}
NSString * firstCharacter = [day substringToIndex:1];;
NSString * secondCharacter = [day substringFromIndex:1];
NSString * chineseFirst;
if (firstCharacter.integerValue == 1) {
chineseFirst = @"十";
}
else if (firstCharacter.integerValue == 2){
chineseFirst = @"廿";
}
else {
chineseFirst = @"三十";
}
NSString * chineseSecond = [dictionary objectForKey:secondCharacter];
NSString * resultString = [NSString stringWithFormat:@"%@%@",chineseFirst,chineseSecond];
return resultString;
}
@end
- 可以把NSDate类型转换成 二零二零年十一月一日这种格式 如果不满足需求 自己可以随意修改