用js中date显示当前日期并转换为汉字形式
时间: 2023-05-19 20:01:10 浏览: 37
非常感谢您的提问!以下是使用 JavaScript 中的 Date 对象来显示当前日期并将其转换为汉字形式的代码:
```javascript
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const cnNums = ['〇', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
const cnMonth = ['十', '二十', '三十'];
let cnYear = '';
for (let i = 0; i < year.toString().length; i++) {
cnYear += cnNums[parseInt(year.toString().charAt(i))];
}
let cnMonthStr = '';
if (month <= 10) {
cnMonthStr = cnNums[month];
} else if (month > 10 && month < 20) {
cnMonthStr = cnMonth[0] + cnNums[month % 10];
} else if (month >= 20) {
cnMonthStr = cnMonth[1] + cnNums[month % 10];
}
let cnDay = '';
if (day <= 10) {
cnDay = cnNums[day];
} else if (day > 10 && day < 20) {
cnDay = cnMonth[0] + cnNums[day % 10];
} else if (day >= 20 && day < 30) {
cnDay = cnMonth[1] + cnNums[day % 10];
} else if (day === 30) {
cnDay = cnMonth[2];
}
console.log(`今天是${cnYear}年${cnMonthStr}月${cnDay}日`);
```
希望这个代码能够帮助到您!