徐向博 / Imin.

JS获取农历日期
作者:Imin 时间:2021-08-30 分类: 前端

JS获取农历日期,代码如下:

const getChineseDate = (time) => {
    let date = time ? new Date(time) : new Date();
    dateString = date.toLocaleString('zh-CN-u-ca-chinese');
    dateString = dateString.replace(/(\d+)\s*?年/, ( x, y) => {
        let result = '';
        result = "甲乙丙丁戊己庚辛壬癸".charAt((y - 4% 10); // 天干
        result += "子丑寅卯辰巳午未申酉戌亥".charAt((y - 4% 12); // 地支
        return result;
    });
    dateString = dateString.split(' ')[0]; // 取年月日
    let g = dateString.substr(0,2+ '';
    let m = dateString.substring(2, dateString.match('').index) + '';
    let d = dateString.match(/\d+/)[0];
    d = d < 11 ? '' + tool.numberToString(d) : tool.numberToString(d);
    let animals = ["","","","","","","","","","","",""];
    let index = date.toLocaleString('zh-CN-u-ca-chinese').substr(0, 4% 12;
    let a = animals[index];
    return { 
        g, // 干支
        m, // 月
        d, // 日
        a  // 生肖
    };
}
上面代码中用到的 tool ,可以在这里找到:JS实现 数字转中文数字



本文标签: js获取农历 日期

雷达探币: 好文章支持一下,博客十年,欢迎回访我的博客 https://coincryptoradar.com 2021-09-15 22:06