1
0
wiki/Static/lunar_data.js

37 lines
1012 B
JavaScript
Raw Normal View History

2024-02-26 16:18:14 +08:00
const https = require("https");
/**
* 获取并格式化当前日期的农历信息
* @returns {Promise<string>} 返回格式化后的农历信息
*/
async function getLunarDate() {
return new Promise((resolve, reject) => {
const url = "https://api.timelessq.com/time";
https
.get(url, (resp) => {
let data = "";
resp.on("data", (chunk) => {
data += chunk;
});
resp.on("end", () => {
const result = JSON.parse(data);
if (result.errno === 0) {
const lunar = result.data.lunar;
const formattedLunarDate = `${lunar.cyclicalYear}${lunar.zodiac}${lunar.cyclicalMonth}${lunar.cyclicalDay}日 农历${lunar.cnMonth}${lunar.cnDay}`;
resolve(formattedLunarDate);
} else {
reject("Failed to fetch lunar date");
}
});
})
.on("error", (err) => {
reject("Error: " + err.message);
});
});
}
module.exports = getLunarDate;