例:2022-08-11 =====> 二〇二二年八月十一日
2022-10-20 =====> 二〇二二年十月二十日
2022-11-21 =====> 二〇二二年十一月二十一日
function convertChineseDate(dateStr) {
let date = new Date(dateStr);
let chinese = ['〇', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
let y = date.getFullYear().toString();
let m = (date.getMonth() + 1).toString();
let d = date.getDate().toString();
let result = '';
//年
for (let i = 0; i < y.length; i++) {
result += chinese[y.charAt(i)];
}
result += '年';
//月
if (m.length == 2) {
let temp = '十';
if (m.charAt(1) != '0') {
temp += chinese[m.charAt(1)];
}
result += temp;
} else {
result += (chinese[m]);
}
result += '月';
//日
if (d.length == 2) {
let temp = '';
if (d.charAt(0) != '1') {
temp += chinese[d.charAt(0)];
}
temp += '十';
if (d.charAt(1) != '0') {
temp += chinese[d.charAt(1)];
}
result += temp;
} else {
result += (chinese[d]);
}
result += '日';
return result;
}
console.log(convertChineseDate('2022-08-11'));