微信小程序日歷/日期選擇插件使用方法詳解
更新時(shí)間:2018年12月28日 11:39:28 作者:偌任
這篇文章主要為大家詳細(xì)介紹了微信小程序日歷和日期選擇插件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
微信小程序日歷選擇器插件點(diǎn)擊日歷日期可以獲取到年月日,具體內(nèi)容如下

wxml
<view class="canlendarBgView">
<view class="canlendarView">
<view class="canlendarTopView">
<view class="leftBgView" bindtap="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="{{index}}">{{item}}</view>
</view>
<view class="dateBgView">
<view wx:if="{{hasEmptyGrid}}" class="dateEmptyView" wx:for="{{empytGrids}}" wx:key="{{index}}" data-idx="{{index}}">
</view>
<view class="dateView" wx:for="{{days}}" wx:key="{{index}}" data-idx="{{index}}" bindtap="dateSelectAction">
<view class="datesView {{index == todayIndex ? 'dateSelectView' : ''}}">{{item}}</view>
</view>
</view>
</view>
<view>點(diǎn)擊日期選擇</view>
</view>
js
//index.js
//獲取應(yīng)用實(shí)例
Page({
data: {
hasEmptyGrid: false,
cur_year: '',
cur_month: '',
},
onLoad(options) {
this.setNowDate();
},
dateSelectAction: function (e) {
var cur_day = e.currentTarget.dataset.idx;
this.setData({
todayIndex: cur_day
})
console.log(`點(diǎn)擊的日期:${this.data.cur_year}年${this.data.cur_month}月${cur_day + 1}日`);
},
setNowDate: function () {
const date = new Date();
const cur_year = date.getFullYear();
const cur_month = date.getMonth() + 1;
const todayIndex = date.getDate() - 1;
console.log(`日期:${todayIndex}`)
const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
this.calculateEmptyGrids(cur_year, cur_month);
this.calculateDays(cur_year, cur_month);
this.setData({
cur_year: cur_year,
cur_month: cur_month,
weeks_ch,
todayIndex,
})
},
getThisMonthDays(year, month) {
return new Date(year, month, 0).getDate();
},
getFirstDayOfWeek(year, month) {
return new Date(Date.UTC(year, month - 1, 1)).getDay();
},
calculateEmptyGrids(year, month) {
const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
let empytGrids = [];
if (firstDayOfWeek > 0) {
for (let i = 0; i < firstDayOfWeek; i++) {
empytGrids.push(i);
}
this.setData({
hasEmptyGrid: true,
empytGrids
});
} else {
this.setData({
hasEmptyGrid: false,
empytGrids: []
});
}
},
calculateDays(year, month) {
let days = [];
const thisMonthDays = this.getThisMonthDays(year, month);
for (let i = 1; i <= thisMonthDays; i++) {
days.push(i);
}
this.setData({
days
});
},
handleCalendar(e) {
const handle = e.currentTarget.dataset.handle;
const cur_year = this.data.cur_year;
const cur_month = this.data.cur_month;
if (handle === 'prev') {
let newMonth = cur_month - 1;
let newYear = cur_year;
if (newMonth < 1) {
newYear = cur_year - 1;
newMonth = 12;
}
this.calculateDays(newYear, newMonth);
this.calculateEmptyGrids(newYear, newMonth);
this.setData({
cur_year: newYear,
cur_month: newMonth
})
} else {
let newMonth = cur_month + 1;
let newYear = cur_year;
if (newMonth > 12) {
newYear = cur_year + 1;
newMonth = 1;
}
this.calculateDays(newYear, newMonth);
this.calculateEmptyGrids(newYear, newMonth);
this.setData({
cur_year: newYear,
cur_month: newMonth
})
}
}
})
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于JAVASCRIPT urldecode URL解碼的問題
JS要實(shí)現(xiàn)單純的編碼解碼輕而易舉,escape、unescape就搞定,但是遇到不是完整的URL轉(zhuǎn)碼,就沒辦法了2012-01-01
Bootstrap Metronic完全響應(yīng)式管理模板學(xué)習(xí)筆記
這篇文章主要為大家分享了Bootstrap Metronic完全響應(yīng)式管理模板學(xué)習(xí)筆記,感興趣的小伙伴們可以參考一下2016-07-07
js正則表達(dá)式最長(zhǎng)匹配(貪婪匹配)和最短匹配(懶惰匹配)用法分析
這篇文章主要介紹了js正則表達(dá)式最長(zhǎng)匹配(貪婪匹配)和最短匹配(懶惰匹配)用法,結(jié)合實(shí)例形式分析了貪婪匹配與懶惰匹配的具體用法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-12-12
KnockoutJS 3.X API 第四章之click綁定
click綁定主要作用是用于DOM元素被點(diǎn)擊時(shí)調(diào)用相關(guān)JS函數(shù)。這篇文章主要介紹了KnockoutJS 3.X API 第四章之click綁定,感興趣的朋友一起看看吧2016-10-10
JS實(shí)現(xiàn)滑動(dòng)門效果的方法詳解
這篇文章主要介紹了JS實(shí)現(xiàn)滑動(dòng)門效果的方法,結(jié)合實(shí)例形式分析了滑動(dòng)門效果的實(shí)現(xiàn)原理、步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-12-12

