微信小程序日历选择器插件点击日历日期可以获取到年月日,具体内容如下
页面结构
view class=' canlendarBgView ' view class=' canlendarView ' view class=' canlendarTopView ' view class=' leftBgView ' bind tap=' handleCalendar ' data-handle=' prev ' view class=' leftView ' 《/view /view view class="centerView"{{cur_year || "--"}} 年 {{cur_month || "--"}} 月/view view class="rightBgView" bindtap="handleCalendar" data-handle="next" view class="rightView"》/view/view/view view class=' weekBgView ' view class=' weekView ' wx : for=' { { weeks _ ch } } ' wx : key=' { { index } } ' data-idx='日期选择视图: ' ' } } ' { { item } }/视图/视图/视图/视图视图点击日期选择/view/viewjs
//index.js//获取应用实例page({ data : { hasemptygrid : false,cur_year: ' ',cur_month: ' ',},OnLoad(options){ this。setNowDate();},日期选择功能:函数(e){ var cur _ day=e . currenttarget。数据集。idx这个。setdata({ todayindex : cur _ day })控制台。日志(` s点击的日期:${this.data.cur_year}年${this.data.cur_month}月${cur_day 1}日`);},setNowDate:函数(){常量日期=新日期();const cur _ year=date。getfull year();const cur _ month=日期。get month()1;const todayIndex=日期。get date()-1;控制台。日志(` 0日期:${todayIndex}`) const weeks_ch=['日', '一', '二', '三', '四', '五', '六'];这个。calculatettemptygrids(cur _ year,cur _ month);this.calculateDays(当前年,当前月);这个。setdata({ cur _ year : cur _ year,cur_month: cur_month,weeks_ch,todayIndex,}) },getThisMonthDays(年,月){返回新日期(年,月,0)。getDate();},getFirstDayOfWeek(年,月){返回新日期(日期。世界协调时(年,月- 1,1))。getDay();},calculateEmptyGrids(年,月){ const第一天fweek=this。获取第一天fweek(年,月);让empytGrids=[];if (firstDayOfWeek 0) { for(让I=0;我第一天上班;I){ empytgrids。推(我);}这个。setdata({ hasemptygrid : true,empytGrids });} else { this。setdata({ hasemptygrid : false,empytgrids :[]});} },计算天数(年,月){ let days=[];const this monthdays=this。获取此月日(年、月);用于(设I=1;i=这个月天;I){天。推(我);}这个。setdata({ days });},handleCalendar(e){ const handle=e . CurrentTarget。数据集。手柄;const cur _ year=this。数据。当前年份;const cur _ month=this。数据。cur _ monthif(handle===' prev '){ 0让new month=cur _ month-1;让newYear=cur _ yearif(新的第1个月){ new year=cur _ year-1;newMonth=12}这个。计算日期(新年、新月份);this.calculateEmptyGrids(新年,新月);这个。setdata({ cur _ year : new year,cur _ month : new month })else {让新月份=cur _ month 1;让newYear=cur _ yearif(新的第12个月){ new year=cur _ year 1;新月份=1;}这个。计算日期(新年、新月份);this.calculateEmptyGrids(新年,新月);这个。setdata({ cur _ year : new year,cur_month: newMonth }) } }})以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。