本插件在原作者(转载地址:http://blog.csdn.net/lengyue1084/article/details/71248778)基础上升级。
components/calendar/calendar.wxml
<view class="page">
<view class="box">
<view class="box-flex">
<view class="flex-item">
<view class="item-content" bindtap="doDay" data-key='left'>
<!-- <view class="glyphicon glyphicon-triangle-left"></view> -->
<image src="./imgs/left_icon.png"></image>
</view>
</view>
<view class="flex-item item-content-current-day">
<view class="item-content">{{currentDate}}</view>
</view>
<view class="flex-item">
<view class="item-content" bindtap="doDay" data-key="right">
<image src="./imgs/right_icon.png"></image>
<!-- <view class="glyphicon glyphicon-triangle-right"></view> -->
</view>
</view>
</view>
<view class="box-flex week">
<view class="flex-item">
<view class="item-content">一</view>
</view>
<view class="flex-item">
<view class="item-content">二</view>
</view>
<view class="flex-item">
<view class="item-content">三</view>
</view>
<view class="flex-item">
<view class="item-content">四</view>
</view>
<view class="flex-item">
<view class="item-content">五</view>
</view>
<view class="flex-item">
<view class="item-content">六</view>
</view>
<view class="flex-item">
<view class="item-content">日</view>
</view>
</view>
<view class="box-flex">
<view class="flex-item {{currentMonth<nowMonth&&nowYear==currentYear?'colorAdd':currentMonth==nowMonth?(vo>=nowData?'':'colorAdd'):''}}"
wx:for="{{currentDayList}}" wx:for-index='key' data-day="{{vo}}" data-index="{{key}}" wx:for-item="vo"
wx:key="{{key}}"
bindtap="{{currentMonth>nowMonth?'selectDay':currentMonth==nowMonth?(nowData<=vo?'selectDay':''):''}}">
<!-- 高亮显示 -->
<view data-day="{{vo}}" data-index="{{key}}"
class="item-content bk-color-day {{select[0].date==vo&¤tMonth==select[0].month?'border_left':''||select[1].date==vo&¤tMonth==select[1].month?'border_Right':''}}"
wx:if="{{select.length==2?(select[0].month==select[1].month?(currentMonth==select[0].month&&select[0].date<=vo&&vo<=select[1].date):(select[0].month==currentMonth&&select[0].date<=vo&&vo||currentMonth==select[1].month&&select[1].date>=vo&&vo||select[0].month<currentMonth&¤tMonth<select[1].month&&vo)):(currentMonth==select[0].month&&vo==select[0].date&&vo)&&nowYear==currentYear}}">
{{vo}}</view>
<view data-day="{{vo}}" data-index="{{key}}" class="item-content {{!selectCSS}}" wx:else>{{vo}}</view>
</view>
</view>
<view class="box-flexs">
<view class="flex-items">
<view class="item-contents">{{starDate}}<text>入住</text></view>
</view>
<view class="flex-items" wx:if="{{countDates}}">
<view class="item-contents item-number">共{{countDates}}晚</view>
</view>
<view class="flex-items">
<view class="item-contents"><text>离开</text>{{endDate}}</view>
</view>
</view>
</view>
</view>
components/calendar/calendar.wxss
/* @import '../../dist/css/icon.wxss'; */
page {
background-color: #2a8cef;
background: -webkit-gradient(linear, 0 0, 0 bottom, from(#2a8cef), to(#8A2BE2));
background: #ccc;
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: stretch;
font-size: 14px;
}
.box {
display: block;
/* margin: 10px; */
background-color: #fff;
padding: 40rpx 30rpx;
border-radius: 20rpx;
}
.box-flex {
display: -webkit-box;
display: -webkit-flex;
display: flex;
flex-wrap: wrap;
}
.flex-item {
flex-flow: nowrap;
flex-grow: 1;
flex-shrink: 1;
width: 14.2%;
}
.item-content {
margin: 10px 0;
padding: 0 10px;
text-align: center;
/* background-color: #000; */
height: 45rpx;
line-height: 45rpx;
color: #201C1D;
/* font-size: 34rpx; */
}
.week .item-content {
color: #F09F2A;
font-weight: bold;
font-size: 28rpx;
}
.bk-color-day {
background-color: #F09F2A;
color: #fff;
}
.item-content-current-day {
flex-grow: 2;
}
.box-flexs {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.item-contents {
/* line-height: 100rpx; */
}
.box-flexs {
font-size: 30rpx;
padding: 45rpx 5rpx 30rpx;
border-top: 1rpx solid #f0f0f0;
margin-top: 20rpx;
}
.item-contents text {
font-size: 24rpx;
padding: 0 10rpx;
color: #656467;
}
.item-number {
color: #F09F2A;
}
.colorAdd .item-content {
color: #999;
}
.border_left {
border-radius: 45rpx 0 0 45rpx;
}
.border_Right {
border-radius: 0 45rpx 45rpx 0;
}
.item-content image {
width: 15rpx;
height: 27rpx;
}
components/calendar/calendar.js
// components/calendar/calendar.js
var app = getApp();
Component({
/**
* 组件的属性列表
*/
properties: {
},
/**
* 组件的初始数据
*/
data: {
currentDate: "2017年05月03日",
dayList: '',
currentDayList: '',
currentObj: '',
currentDay: '',
nowData: '',
nowYear: '',
nowMonth: '',
select: [],
countDates: '',
//日期初始化选中样式
selectCSS: 'bk-color-day',
starDate: '',
endDate: ''
},
/**
* 组件的方法列表
*/
ready: function () {
var that = this;
var currentObj = this.getCurrentDayString()
// + currentObj.getDate()
this.setData({
currentDate: currentObj.getFullYear() + '年' + (currentObj.getMonth() + 1) + '月',
currentDay: currentObj.getDate(),
currentObj: currentObj,
/* 获取当前的年、月 */
currentYear: currentObj.getFullYear(),
currentMonth: (currentObj.getMonth() + 1),
nowData: currentObj.getDate(),
nowYear: currentObj.getFullYear(),
nowMonth: (currentObj.getMonth() + 1),
})
this.setSchedule(currentObj);
console.log(this.data.currentDayList,this.data.nowYear,999999)
},
methods: {
doDay: function (e) {
var that = this;
var currentObj = that.data.currentObj
var Y = currentObj.getFullYear();
var m = currentObj.getMonth() + 1;
var d = currentObj.getDate();
var str = ''
if (e.currentTarget.dataset.key == 'left') {
m -= 1
if (m <= 0) {
str = (Y - 1) + '/' + 12 + '/' + d
} else {
str = Y + '/' + m + '/' + d
}
} else {
m += 1
if (m <= 12) {
str = Y + '/' + m + '/' + d
} else {
str = (Y + 1) + '/' + 1 + '/' + d
}
}
currentObj = new Date(str)
// + currentObj.getDate()
this.setData({
currentDate: currentObj.getFullYear() + '年' + (currentObj.getMonth() + 1) + '月',
currentObj: currentObj,
/* 获取当前的年、月 */
currentYear: currentObj.getFullYear(),
currentMonth: (currentObj.getMonth() + 1),
})
console.log("选择当前年:" + that.data.currentYear);
console.log("选择当前月:" + that.data.currentMonth);
this.setSchedule(currentObj);
},
getCurrentDayString: function () {
var objDate = this.data.currentObj
if (objDate != '') {
return objDate
} else {
var c_obj = new Date()
var a = c_obj.getFullYear() + '/' + (c_obj.getMonth() + 1) + '/' + c_obj.getDate()
return new Date(a)
}
},
setSchedule: function (currentObj) {
var that = this
var m = currentObj.getMonth() + 1
var Y = currentObj.getFullYear()
var d = currentObj.getDate();
var dayString = Y + '/' + m + '/' + currentObj.getDate()
var currentDayNum = new Date(Y, m, 0).getDate()
var currentDayWeek = currentObj.getUTCDay() + 1
var result = currentDayWeek - (d % 7 - 1);
var firstKey = result <= 0 ? 7 + result : result;
var currentDayList = [];
var f = 0
for (var i = 0; i < 42; i++) {
let data = []
if (i < firstKey - 1) {
currentDayList[i] = ''
} else {
if (f < currentDayNum) {
currentDayList[i] = f + 1;
f = currentDayList[i]
} else if (f >= currentDayNum) {
currentDayList[i] = ''
}
}
}
that.setData({
currentDayList: currentDayList
})
},
//选择具体日期方法--xzz1211
selectDay: function (e) {
var that = this;
console.log(e);
var starDate = '',
endDate = '',
that = this,
countDate = ''
var select = this.data.select
var obj = {}
obj.month = this.data.currentMonth
obj.date = e.target.dataset.day
if (select.length < 2) {
select.push(obj)
starDate = select[0].month + '月' + select[0].date + '日'
if (select.length == 2) {
endDate = select[1].month + '月' + select[1].date + '日'
if (select[0].month == select[1].month) {
if (select[1].date <= select[0].date) {
console.log('第二个小于第一个')
select = []
select.push(obj)
starDate = select[0].month + '月' + select[0].date + '日'
endDate = ''
console.log(select, 5555)
}
} else if (select[0].month < select[1].month) {
console.log('第二个小于第一个')
endDate = select[1].month + '月' + select[1].date + '日'
} else {
select = []
select.push(obj)
endDate = ''
starDate = select[0].month + '月' + select[0].date + '日'
console.log(select, 222)
}
if (starDate && endDate) {
countDate = this.getDaysBetween(that.data.currentYear + '-' + select[0].month + '-' + select[0].date, that.data.currentYear + '-' + select[1].month + '-' + select[1].date)
}
}
} else {
console.log('第三个')
select = []
select.push(obj)
starDate = select[0].month + '月' + select[0].date + '日'
}
console.log(select, starDate, endDate, 88888, countDate)
that.setData({
currentDay: e.target.dataset.day, //选择的数据,非真实当前日期
currentDa: e.target.dataset.day, //选择某月具体的一天
currentDate: that.data.currentYear + '年' + that.data.currentMonth + '月',
endDate: endDate,
starDate: starDate,
select: select,
countDates: countDate
})
// + e.target.dataset.day,//真实选择数据
console.log("当前选择日期:" + that.data.currentDate);
},
getDaysBetween: function (dateString1, dateString2) {
var startDate = Date.parse(dateString1);
var endDate = Date.parse(dateString2);
if (startDate > endDate) {
return 0;
}
if (startDate == endDate) {
return 1;
}
var days = parseInt((endDate - startDate) / (1 * 24 * 60 * 60 * 1000));
return days;
},
}
})
在父组件中引用 wxml
<checkin-picker />
json中
{
"usingComponents": {
"checkin-picker": "/components/calendar/calendar"
}