Hongmu Notes
Home Language Notes jQuery code to output the current date and lunar calendar in the specified id box
Language Notes JavaScript

jQuery code to output the current date and lunar calendar in the specified id box

jQuery code to output the current date and lunar calendar in the specified id box

The invoked interface is: China Astronomical Observatory Lunar Calendar Data Interface.

Further features – to be developed in-house.

jQuery code:

$(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 code:

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

 

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

js move the mouse up to modify the style attribute

js move the mouse up to modify the style attribute Language Notes JavaScript

When the mouse hovers over the element, the color changes to red; when the mouse leaves the element, the color changes to black. To retrieve an element by its ID: `document.getElementById()`; Event handler for mouse hover: `onmouseover`; Event handler for mouse leave: `onmouseout`.<!DOCTYPE html><html lang= "en">...
👁 130

Recommended reading

(Adaptive mobile version) Responsive full-home renovation and custom home furnishings website – PBootCMS template; Download source code for a green home renovation company website – 0261

(Adaptive mobile version) Responsive full-home renovation and custom home furnishings website – PBootCMS template; Download source code for a green home renovation company website – 0261 Practical Collection pbootcms Template

This is an adaptive, responsive website template built with PbootCMS, designed for home renovation customization and home furnishings businesses. Its eco-friendly design makes it ideal for displaying case studies and products from renovation companies or whole-house customization brands. It helps home improvement businesses attract homeowners online and enhance their brand image. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles...
👁 37
Responsive food and pastry business website template – 1194

Responsive food and pastry business website template – 1194 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for food and pastry businesses. Its warm and inviting design is perfect for showcasing pastries, brand stories, product ranges, and franchise information. It helps baking brands attract consumers and franchisees online while enhancing their brand awareness. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 51
Responsive solid wood furniture brand website template 0474

Responsive solid wood furniture brand website template 0474 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the solid wood furniture industry, featuring a premium and natural design style that effectively showcases solid wood furniture products, brand stories, design concepts, and customized services. It helps solid wood furniture brands showcase their products online and attract consumers who value a quality lifestyle. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS...
👁 61
(Adaptive Mobile Version) Sandbox Model Website Template – 3D Architectural Model Website Source Code Download – 0949

(Adaptive Mobile Version) Sandbox Model Website Template – 3D Architectural Model Website Source Code Download – 0949 Practical Collection pbootcms Template

A PbootCMS website template designed for sand table models and 3D architectural models, compatible with both PC and WAP devices. Featuring a professional artistic design style, it is ideal for showcasing sand table models, architectural models, and design works. This template helps model design companies demonstrate their craftsmanship online and attract clients in the real estate and design industries. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5....
👁 38
(Adaptive mobile version) Responsive criminal defense legal information website – PBootCMS template; Legal firm website source code download – 0262

(Adaptive mobile version) Responsive criminal defense legal information website – PBootCMS template; Legal firm website source code download – 0262 Practical Collection pbootcms Template

An adaptive, responsive PbootCMS website template for criminal defense and legal information services designed for mobile devices. Its professional and formal design makes it ideal for law firms and legal consultation platforms to showcase their services. This template helps legal service providers establish an authoritative online presence and attract clients with legal needs. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.c...
👁 29