Skip to content

Commit 30986a2

Browse files
committed
add lunarDate
1 parent b5e4189 commit 30986a2

File tree

6 files changed

+346
-14
lines changed

6 files changed

+346
-14
lines changed

pages/index/component/calendar/calendar.js

Lines changed: 158 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,28 @@ Page({
1111
space: 0,
1212
week: "",
1313
dateString: "",
14-
spot: ['2021/11/6', '2020/8/9', '2020/8/20', '2020/9/12']
14+
spot: ['2021/11/6', '2020/8/9', '2020/8/20', '2020/9/12'],
15+
// 农历
16+
Nongli: 0,
17+
lunarInfo: [0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554,
18+
0x056a0, 0x09ad0, 0x055d2, 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0,
19+
0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566,
20+
0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550,
21+
0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0,
22+
0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263,
23+
0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0,
24+
0x195a6, 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, 0x04af5,
25+
0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960, 0x0d954, 0x0d4a0,
26+
0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9,
27+
0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0,
28+
0x0d260, 0x0ea65, 0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520,
29+
0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0
30+
],
31+
chineseNumber: [
32+
"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "冬", "腊"
33+
],
34+
lunarDate: '',
35+
1536
},
1637

1738
/**
@@ -22,6 +43,8 @@ Page({
2243
this.getSpace();
2344
this.createDay();
2445
this.emit();
46+
// 获取当前日期的农历日期
47+
this.getLunarDate(new Date());
2548
},
2649

2750
/**
@@ -89,7 +112,14 @@ Page({
89112
time.setDate(0);
90113
let len = time.getDate();
91114
for (let i = 1; i <= len; i++) {
92-
day.push(i);
115+
time.setDate(i);
116+
// 获取农历日期
117+
let lunarDate = this.getLunarDate(time);
118+
day.push({
119+
"day": i,
120+
"lunar": lunarDate
121+
});
122+
// day.push(i);
93123
}
94124
this.setData({
95125
day
@@ -172,6 +202,8 @@ Page({
172202
today
173203
} = this.data;
174204
let time = new Date(year, month - 1, today);
205+
// 获取选中日期的农历日期
206+
this.getLunarDate(time);
175207
let week = time.getDay();
176208
week = this.formatWeek(week)
177209
this.setData({
@@ -214,4 +246,128 @@ Page({
214246
dateString: e.detail.dateString
215247
})
216248
},
249+
// 获取农历日期相关函数
250+
getLunarDate: function (curDate) {
251+
var self = this;
252+
var yearCyl, monCyl, dayCyl;
253+
var leapMonth = 0;
254+
var date = new Date('1900/1/31');
255+
// var curDate = new Date();
256+
// 求出和1900年1月31日相差的天数
257+
var offset = parseInt((curDate.getTime() - date.getTime()) / 86400000);
258+
dayCyl = offset + 40;
259+
monCyl = 14;
260+
// 用offset减去每农历年的天数
261+
// 计算当天是农历第几天
262+
// i最终结果是农历的年份
263+
// offset是当年的第几天
264+
var iYear, daysOfYear = 0;
265+
for (iYear = 1900; iYear < 2050 && offset > 0; iYear++) {
266+
daysOfYear = self.yearDays(iYear);
267+
offset -= daysOfYear;
268+
monCyl += 12;
269+
}
270+
if (offset < 0) {
271+
offset += daysOfYear;
272+
iYear--;
273+
monCyl -= 12;
274+
}
275+
yearCyl = iYear - 1864;
276+
leapMonth = self.leapMonth(iYear); // 闰哪个月,1-12
277+
var leap = false; // 默认值
278+
// 用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天
279+
var iMonth, daysOfMonth = 0;
280+
for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) {
281+
// 闰月
282+
if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) {
283+
--iMonth;
284+
leap = true;
285+
daysOfMonth = self.leapDays(iYear);
286+
} else
287+
daysOfMonth = self.monthDays(iYear, iMonth);
288+
289+
offset -= daysOfMonth;
290+
// 解除闰月
291+
if (leap && iMonth == (leapMonth + 1))
292+
leap = false;
293+
if (!leap)
294+
monCyl++;
295+
}
296+
// offset为0时,并且刚才计算的月份是闰月,要校正
297+
if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) {
298+
if (leap) {
299+
leap = false;
300+
} else {
301+
leap = true;
302+
--iMonth;
303+
--monCyl;
304+
}
305+
}
306+
// offset小于0时,也要校正
307+
if (offset < 0) {
308+
offset += daysOfMonth;
309+
--iMonth;
310+
--monCyl;
311+
}
312+
var newday = self.getChinaDayString(offset + 1);
313+
var newmonth = self.data.chineseNumber[iMonth - 1];
314+
self.setData({
315+
lunarDate: newmonth + '月' + newday
316+
})
317+
//把日期整合成一个字符串
318+
var nongli = iYear + '年' + newmonth + '月' + newday;
319+
//把nongli赋值给date里的Nongli,才能在html中使用
320+
this.setData({
321+
Nongli: nongli
322+
})
323+
if (newday == '初一') {
324+
return newmonth + '月'
325+
} else {
326+
return newday
327+
}
328+
},
329+
330+
//***上面的方法调用下面的方法***
331+
332+
yearDays: function (y) {
333+
var self = this;
334+
var i, sum = 348;
335+
for (i = 0x8000; i > 0x8; i >>= 1) {
336+
if ((self.data.lunarInfo[y - 1900] & i) != 0)
337+
sum += 1;
338+
}
339+
return (sum + self.leapDays(y));
340+
},
341+
leapDays: function (y) {
342+
var self = this;
343+
if (self.leapMonth(y) != 0) {
344+
if ((self.data.lunarInfo[y - 1900] & 0x10000) != 0)
345+
return 30;
346+
else
347+
return 29;
348+
} else
349+
return 0;
350+
},
351+
leapMonth: function (y) {
352+
var self = this;
353+
return self.data.lunarInfo[y - 1900] & 0xf;
354+
},
355+
monthDays: function (y, m) {
356+
if ((this.data.lunarInfo[y - 1900] & (0x10000 >> m)) == 0)
357+
return 29;
358+
else
359+
return 30;
360+
},
361+
getChinaDayString: function (day) {
362+
var self = this;
363+
var chineseTen = ["初", "十", "廿", "卅"];
364+
var n = day % 10 == 0 ? 9 : day % 10 - 1;
365+
if (day > 30)
366+
return "";
367+
if (day == 10)
368+
return "初十";
369+
else
370+
return chineseTen[parseInt(day / 10)] + self.data.chineseNumber[n];
371+
},
372+
217373
})

pages/index/component/calendar/calendar.wxml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
</view>
2929
<view class="main-center">
3030
<view wx:for="{{space}}" wx:key="item"></view>
31-
<view wx:for="{{day}}" wx:key="item" data-today="{{item}}" bindtap="click" class="{{today == item? 'bg-blue padding radius light text-bold' : ''}}">
32-
{{item}}
31+
<view wx:for="{{day}}" wx:key="item" data-today="{{item.day}}" bindtap="click" class="{{today == item.day? 'bg-blue padding radius light text-bold' : ''}}" style="height: 76rpx;">
32+
<view class="flex flex-wrap">{{item.day}}
33+
<view class="text-xs" style="margin-top: -10rpx; padding-bottom: 6rpx;">{{item.lunar}}</view>
34+
</view>
3335
</view>
3436
</view>
3537
</view>
@@ -39,7 +41,7 @@
3941
<view class="grid col-1 bg-white margin-bottom margin-lr-sm radius-lg">
4042
<view class="padding-sm">
4143
<view class="padding-lg text-center shadow-blur l-card">
42-
选中日期为:<text class="text-blue">{{year}}</text> 年 <text class="text-blue">{{month}}</text> 月 <text class="text-blue">{{today}}</text> 日,周<text class="text-blue">{{week}}</text>
44+
选中日期为:<text class="text-blue">{{year}}</text> 年 <text class="text-blue">{{month}}</text> 月 <text class="text-blue">{{today}}</text> 日,周<text class="text-blue">{{week}}</text>, 农历:<text class="text-blue">{{lunarDate}}</text>
4345
</view>
4446
</view>
4547
</view>
@@ -80,9 +82,11 @@
8082
</view>
8183
<view class="main-center-mini">
8284
<view wx:for="{{space}}" wx:key="item"></view>
83-
<view wx:for="{{day}}" wx:key="item" data-today="{{item}}" bindtap="click" class="{{today == item? 'bg-blue padding radius light text-bold' : ''}}">
84-
{{item}}
85-
</view>
85+
<view wx:for="{{day}}" wx:key="item" data-today="{{item.day}}" bindtap="click" class="{{today == item.day? 'bg-blue padding radius light text-bold' : ''}}" style="height: 76rpx;">
86+
<view class="flex flex-wrap">{{item.day}}
87+
<view class="text-xs" style="margin-top: -10rpx; padding-bottom: 6rpx;">{{item.lunar}}</view>
88+
</view>
89+
</view>
8690
</view>
8791
</view>
8892
</view>
@@ -96,7 +100,7 @@
96100
</view>
97101
</view>
98102

99-
<view class="cu-bar bg-white">
103+
<view class="cu-bar bg-white margin-top">
100104
<view class="action">
101105
<text class="cuIcon-titles text-blue"></text>日历样式二
102106
</view>

0 commit comments

Comments
 (0)