把当前时间用js转换成后面这种格式 2023年5月11日 星期四 农历 癸卯年三月廿三
时间: 2023-06-19 19:03:38 浏览: 340
新年红包,兔年HTML红包页面.zip
5星 · 资源好评率100%
可以使用第三方库LunarCalendar.js来实现,具体步骤如下:
1. 引入LunarCalendar.js库
```html
<script src="https://unpkg.com/lunar-calendar/dist/lunar-calendar.js"></script>
```
2. 获取当前时间
```javascript
var now = new Date();
```
3. 转换为农历时间
```javascript
var lunar = calendar.solar2lunar(now.getFullYear(), now.getMonth() + 1, now.getDate());
```
4. 格式化输出
```javascript
var weekDays = ['日', '一', '二', '三', '四', '五', '六'];
var yearName = calendar.toChinaYear(lunar.lYear);
var monthName = calendar.toChinaMonth(lunar.lMonth);
var dayName = calendar.toChinaDay(lunar.lDay);
var result = now.getFullYear() + '年' + (now.getMonth() + 1) + '月' + now.getDate() + '日 ' + '星期' + weekDays[now.getDay()] + ' 农历 ' + yearName + monthName + dayName;
console.log(result);
```
完整代码如下:
```javascript
<script src="https://unpkg.com/lunar-calendar/dist/lunar-calendar.js"></script>
<script>
var now = new Date();
var lunar = calendar.solar2lunar(now.getFullYear(), now.getMonth() + 1, now.getDate());
var weekDays = ['日', '一', '二', '三', '四', '五', '六'];
var yearName = calendar.toChinaYear(lunar.lYear);
var monthName = calendar.toChinaMonth(lunar.lMonth);
var dayName = calendar.toChinaDay(lunar.lDay);
var result = now.getFullYear() + '年' + (now.getMonth() + 1) + '月' + now.getDate() + '日 ' + '星期' + weekDays[now.getDay()] + ' 农历 ' + yearName + monthName + dayName;
console.log(result);
</script>
```
阅读全文