jQuery代码,指定id盒子内输出当前日期农历

调用的接口为:中国天文台农历数据接口

更多功能自个研究

jQuery代码:

$(document).ready(function() {
    var resultElement = $("#result");

    function getDayInfo(date) {
        var apiUrl = "https://api.asilu.com/lunar";
        var requestData = {
            data: date,
            callback: "processResponse" // 回调函数的名称
        };

        // 创建全局的回调函数,用于处理响应
        window.processResponse = function(response) {
            var weekday = new Date(date).getDay();
            var weekdayStr = "星期" + numToChinese(weekday);

            var lunarDate = response.lunar_year_chinese + "年" + response.lunar_month_chinese + response.lunar_day_chinese;
            var solarTerm = response.term;

            var resultStr = date + "   " + weekdayStr + "   农历" + lunarDate;

            resultElement.html(resultStr);
        };

        // 动态创建一个 <script> 标签来发送请求
        var script = document.createElement("script");
        script.src = apiUrl + "?data=" + encodeURIComponent(date) + "&callback=processResponse";
        document.head.appendChild(script);
    }

    function numToChinese(num) {
        var chineseNums = ["日", "一", "二", "三", "四", "五", "六"];
        return chineseNums[num];
    }

    var currentDate = new Date();
    var formattedDate = currentDate.getFullYear() + "-" + (currentDate.getMonth() + 1) + "-" + currentDate.getDate();

    getDayInfo(formattedDate);
});

html代码:

<div id="result"></div>

 

© 版权声明
THE END
喜欢就支持一下吧
点赞7
评论 抢沙发

请登录后发表评论

    暂无评论内容