微信小程序农阳历日历选择器农阳历日期互转

文章主要实现微信小程序中农历、阳历日期选择器 &选中日期同时获取它对应的农/阳历日期

微信小程序农阳历日历选择器农阳历日期互转_第1张图片

因项目需求所以我在demo中实现选中日期并获取相应的农/阳历日期,实现的主要代码在selectdate.js代码的61-

if (this.data.selectDateType === 1) {//阳历
                  lastYear = this.data.selected_value[0] + MIN_LUNAR_YEAY;
                  lastMonth = this.data.selected_value[1] + 1;//真实的月份
                  lastDay = this.data.selected_value[2] + 1;
                  var d = new Date(lastYear, this.data.selected_value[1], lastDay);
                  var lunarDate = calendarConverter.solar2lunar(d)//阳历日期转农历日期
                  console.log("阳转农:", lunarDate.lYear + "年" + lunarDate.lMonth + "月" + lunarDate.lDay);
                  console.log("阳  历:", lastYear + "年" + lastMonth + "月" + lastDay);
            } else {//农历
                  
                  var launch_Mouth = this.data.lunar_month[this.data.lunar_selected_value[1]].name;
                  launchYear = this.data.lunar_selected_value[0] + MIN_LUNAR_YEAY;
                  if (launch_Mouth.indexOf("闰") === -1) {//不是闰月
                        isLeap = false;
                        launchMonth = month_lunar.indexOf(launch_Mouth.substring(0, 1));
                  } else {
                        isLeap = true;
                        launchMonth = month_lunar.indexOf(launch_Mouth.substring(1, 2));
                  }
                  launchDay = this.data.lunar_selected_value[2] + 1;
                  console.log("农  历:", launchYear + "年" + launch_Mouth + launchDay);
                  var data = {
                      launchYear: launchYear,
                      launchMonth: launchMonth,
                      launchDay: launchDay
                  }
                  var dEx = calendarConverter.lunar2solar(data, isLeap);//农历转阳历代码
                  lastYear = dEx.sYear;//农转阳(年)
                  lastMonth = dEx.sMonth;//农转阳(月)
                  lastDay = dEx.sDay;//农转阳(日)
                  console.log("农转阳:", lastYear + "年" + lastMonth + "月" + lastDay);
            }


lastYear = dEx.sYear;//农转阳(年) lastMonth = dEx.sMonth;//农转阳(月) lastDay = dEx.sDay;//农转阳(日) console.log("农转阳:", lastYear + "年" + lastMonth + "月" + lastDay);}
calender.js文件中185行调整默认阳历选择器年月日&234;行调整默认阴历选择器

var y = 1992;

var m = 3;//月份从零开始

var d = 23;


demo的下载地址:

https://download.csdn.net/download/u013125372/10378776

你可能感兴趣的:(微信小程序)