JS获取农历日期
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 // 生肖
};
}