当前位置:网站首页>Uniapp native JS to convert the Gregorian calendar to the lunar calendar
Uniapp native JS to convert the Gregorian calendar to the lunar calendar
2022-06-10 18:28:00 【Sit and wait for the sunset time】
The code idea is in the following blog , I'm just recording how I got there uniapp Used in
First step Find the information
Click the original link provided by me , Slip to the bottom , Click on github link

The second step Copy build lunar.js
Select all assignment , Then put it on your uniapp There are special tools in the project js In the folder of

I am because the main package of wechat applet can only 2M, So subcontracting was adopted , On the subcontracted lib Folder

The third step rewrite lunar.js
rewrite lunar.js, So that the method can be import
Rewriting is simple , Just at the bottom , Add the following code , namely , Every one of them function, Can be export
export default {
sloarToLunar,
hasLeapMonth,
leapMonthDays,
lunarYearDays,
lunarYearMonths,
getTianGan,
getDiZhi
};Step four Use sloarToLunar, Realize the conversion from the Gregorian calendar to the lunar calendar
import lunarUtil from './lib/lunar.js';
export default {
data() {
return {
calendarData: '',
},
}
},
onShow() {
const date = new Date()
let lunar = lunarUtil.sloarToLunar(date.getFullYear(), date.getMonth() + 1, date.getDate())
this.calendarData = lunar.lunarMonth + " month " + lunar.lunarDay + " " + lunar.lunarYear + " year "
}
}Step five effect

The attachment
/* Thinking of the code from Gregorian calendar to lunar calendar :
1、 Set up a lunar year query table
2、 Calculate the number of days between the input Gregorian date and the Gregorian base
3、 Start traversing the lunar calendar query table from the lunar calendar benchmark , Calculate the number of days in each year since the lunar calendar benchmark , And subtract the number of days from each other , Determine the lunar year
4、 The remaining difference days and the days of each lunar month are used to determine the lunar month
5、 Use the remaining difference days to determine which day of the lunar calendar */
// Lunar calendar 1949-2100 Year query table
let lunarYearArr = [
0x0b557, //1949
0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5b0, 0x14573, 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0, //1950-1959
0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, //1960-1969
0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 0x195a6, //1970-1979
0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, //1980-1989
0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, //1990-1999
0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, //2000-2009
0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, //2010-2019
0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, //2020-2029
0x05aa0, 0x076a3, 0x096d0, 0x04afb, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, //2030-2039
0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0, //2040-2049
0x14b63, 0x09370, 0x049f8, 0x04970, 0x064b0, 0x168a6, 0x0ea50, 0x06b20, 0x1a6c4, 0x0aae0, //2050-2059
0x0a2e0, 0x0d2e3, 0x0c960, 0x0d557, 0x0d4a0, 0x0da50, 0x05d55, 0x056a0, 0x0a6d0, 0x055d4, //2060-2069
0x052d0, 0x0a9b8, 0x0a950, 0x0b4a0, 0x0b6a6, 0x0ad50, 0x055a0, 0x0aba4, 0x0a5b0, 0x052b0, //2070-2079
0x0b273, 0x06930, 0x07337, 0x06aa0, 0x0ad50, 0x14b55, 0x04b60, 0x0a570, 0x054e4, 0x0d160, //2080-2089
0x0e968, 0x0d520, 0x0daa0, 0x16aa6, 0x056d0, 0x04ae0, 0x0a9d4, 0x0a2d0, 0x0d150, 0x0f252, //2090-2099
0x0d520 //2100
],
lunarMonth = [' just ', ' Two ', ' 3、 ... and ', ' Four ', ' 5、 ... and ', ' 6、 ... and ', ' 7、 ... and ', ' 8、 ... and ', ' Nine ', ' Ten ', ' In the winter ', ' Wax '],
lunarDay = [' One ', ' Two ', ' 3、 ... and ', ' Four ', ' 5、 ... and ', ' 6、 ... and ', ' 7、 ... and ', ' 8、 ... and ', ' Nine ', ' Ten ', ' first ', ' Twenty '],
tianGan = [' nail ', ' B ', ' C ', ' Ding ', ' E ', ' Oneself ', ' Geng ', ' simba ', ' Ren ', ' Dec. '],
diZhi = [' Son ', ' ugly ', ' Yin ', ' Mao ', ' Chen" ', ' Already ', ' Noon ', ' not ', ' Shen ', ' Unitary ', ' Xu ', ' Hai '];
// Gregorian to lunar functions
function sloarToLunar(sy, sm, sd) {
// The entered month minus 1 Handle
sm -= 1;
// Calculate the days difference from the Gregorian calendar benchmark
// Date.UTC() Returns the distance from the Gregorian calendar 1970 year 1 month 1 Milliseconds per day , The incoming month needs to be subtracted 1
let daySpan = (Date.UTC(sy, sm, sd) - Date.UTC(1949, 0, 29)) / (24 * 60 * 60 * 1000) + 1;
let ly, lm, ld;
// Determine the lunar year of the output
for (let j = 0; j < lunarYearArr.length; j++) {
daySpan -= lunarYearDays(lunarYearArr[j]);
if (daySpan <= 0) {
ly = 1949 + j;
// Get the remaining days after the lunar year is determined
daySpan += lunarYearDays(lunarYearArr[j]);
break
}
}
// Determine the lunar month for output
for (let k = 0; k < lunarYearMonths(lunarYearArr[ly - 1949]).length; k++) {
daySpan -= lunarYearMonths(lunarYearArr[ly - 1949])[k];
if (daySpan <= 0) {
// When there are leap months , The array length of the month will become 13, therefore , When the leap month is less than or equal to k when ,lm No need to add 1
if (hasLeapMonth(lunarYearArr[ly - 1949]) && hasLeapMonth(lunarYearArr[ly - 1949]) <= k) {
if (hasLeapMonth(lunarYearArr[ly - 1949]) < k) {
lm = k;
} else if (hasLeapMonth(lunarYearArr[ly - 1949]) === k) {
lm = ' Leap ' + k;
} else {
lm = k + 1;
}
} else {
lm = k + 1;
}
// Get the remaining days after the lunar month is determined
daySpan += lunarYearMonths(lunarYearArr[ly - 1949])[k];
break
}
}
// Determine which day of the lunar calendar to output
ld = daySpan;
// Convert the calculated lunar month into Chinese character month , A leap month needs to be preceded by a leap word
if (hasLeapMonth(lunarYearArr[ly - 1949]) && (typeof(lm) === 'string' && lm.indexOf(' Leap ') > -1)) {
lm = ` Leap ${lunarMonth[/\d/.exec(lm) - 1]}`
} else {
lm = lunarMonth[lm - 1];
}
// Convert the calculated lunar year to the year of heavenly stems and Earthly Branches
ly = getTianGan(ly) + getDiZhi(ly);
// Convert the calculated lunar calendar days into Chinese characters
if (ld < 11) {
ld = `${lunarDay[10]}${lunarDay[ld-1]}`
} else if (ld > 10 && ld < 20) {
ld = `${lunarDay[9]}${lunarDay[ld-11]}`
} else if (ld === 20) {
ld = `${lunarDay[1]}${lunarDay[9]}`
} else if (ld > 20 && ld < 30) {
ld = `${lunarDay[11]}${lunarDay[ld-21]}`
} else if (ld === 30) {
ld = `${lunarDay[2]}${lunarDay[9]}`
}
console.log(ly, lm, ld);
return {
lunarYear: ly,
lunarMonth: lm,
lunarDay: ld,
}
}
// Calculate whether there are leap months in the lunar year , The parameter is used to store the Chinese lunar year 16 Base number
// The lunar year information is 16 Base store , among 16 The last of the hexadecimal 1 Bits can be used to determine whether there is a leap month
function hasLeapMonth(ly) {
// obtain 16 The last of the hexadecimal 1 position , Need to use & And operators
if (ly & 0xf) {
return ly & 0xf
} else {
return false
}
}
// If there is a leap month , Calculate the number of leap months in the lunar calendar , The parameter is used to store the Chinese lunar year 16 Base number
// The lunar year information is 16 Base store , among 16 Hexadecimal th 1 position (0x With the exception of ) It can be used to indicate whether a leap month is a big month or a small month
function leapMonthDays(ly) {
if (hasLeapMonth(ly)) {
// obtain 16 Hexadecimal th 1 position (0x With the exception of )
return (ly & 0xf0000) ? 30 : 29
} else {
return 0
}
}
// Calculate the total number of days in a lunar year , The parameter is used to store the Chinese lunar year 16 Base number
// The lunar year information is 16 Base store , among 16 Hexadecimal th 2-4 position (0x With the exception of ) It can be used to indicate whether a normal month is a big month or a small month
function lunarYearDays(ly) {
let totalDays = 0;
// Get the number of days in a normal month , Sum up
// obtain 16 Hexadecimal th 2-4 position , Need to use >> Shift Operators
for (let i = 0x8000; i > 0x8; i >>= 1) {
let monthDays = (ly & i) ? 30 : 29;
totalDays += monthDays;
}
// If there is a leap month , You need to add the number of days of leap month to
if (hasLeapMonth(ly)) {
totalDays += leapMonthDays(ly);
}
return totalDays
}
// Get the days of each month in the lunar calendar
// Parameters need to be passed in 16 Decimal value
function lunarYearMonths(ly) {
let monthArr = [];
// Get the number of days in a normal month , To add to monthArr Array
// obtain 16 Hexadecimal th 2-4 position , Need to use >> Shift Operators
for (let i = 0x8000; i > 0x8; i >>= 1) {
monthArr.push((ly & i) ? 30 : 29);
}
// If there is a leap month , You need to add the number of days of leap month to
if (hasLeapMonth(ly)) {
monthArr.splice(hasLeapMonth(ly), 0, leapMonthDays(ly));
}
return monthArr
}
// Convert the lunar year to tiangan , The parameter is lunar year
function getTianGan(ly) {
let tianGanKey = (ly - 3) % 10;
if (tianGanKey === 0) tianGanKey = 10;
return tianGan[tianGanKey - 1]
}
// Turn the lunar year into a local branch , The parameter is lunar year
function getDiZhi(ly) {
let diZhiKey = (ly - 3) % 12;
if (diZhiKey === 0) diZhiKey = 12;
return diZhi[diZhiKey - 1]
}
export default {
sloarToLunar,
hasLeapMonth,
leapMonthDays,
lunarYearDays,
lunarYearMonths,
getTianGan,
getDiZhi
};
边栏推荐
- VMware vCenter 各版本号对照表
- 换根呀呀啊呀
- 【ceph】ceph配置源码分析|common/config.*
- 两部门发文明确校外培训机构消防安全条件
- Detailed explanation of MySQL windowing function
- After the qtmqtt source code compilation is set to keepalive, the Ping package timeout error does not return a problem repair (qmqtt:: mqttnopingresponse, qmqtt:: clientprivate:: onpingtimeo)
- 【QNX Hypervisor 2.2 用户手册】3.2.1 VM配置语法
- IP总结(TCP/IP卷1和卷2)
- Red vertical left side menu navigation code
- XML & XPath parsing
猜你喜欢
随机推荐
C语言---1 C语言认知
光储直柔配电系统浅析
Group a large number of files in the same folder into multiple groups according to settings
Memory pool principle I (based on the whole block)
改变世界的开发者丨玩转“俄罗斯方块”的瑶光少年
Aesthetic experience (episode 237) Luo Guozheng
“双碳”背景下 数据中心规划设计的新趋势及展望
内存池原理一(基于整块)
Cdga| six key points of data governance for industrial enterprises
Postman interface test tool
【技术分析】探讨大世界游戏的制作流程及技术——前期流程篇
muduo源码剖析——以三个切片浅析muduo库代码设计的严谨性、高效性与灵活性
c语言---4 初识常量
连续六年稳居中国SDN(软件)市场份额第一
阅读micropyton源码-添加C扩展类模块(4)
XML&Xpath解析
微信小程序仿陶票票课程设计
干货 | 一文搞定 uiautomator2 自动化测试工具使用
c语言学习回顾---1 基础知识回顾
Research on next generation distributed file system









