云迈博客

您现在的位置是:首页 > 前端技术 > 微信小程序 > 正文

微信小程序

微信小程序-时间选择器的联动

徐颖2020-10-20微信小程序332
年月日时。constdate=newDate();constyears=[];constmonths=[];constdays=[];consthours=[

年月日时。
const date = new Date();
const years = [];
const months = [];
const days = [];
const hours = [];
// const minutes = [];
//获取年
for (let i = date.getFullYear(); i <= date.getFullYear() + 10; i++) {
years.push(“” + i);
}
//获取月份
for (let i1 = date.getMonth()+1; i1 <= 12; i1++) {
if (i1 < 10) {
i1 = “0” + i1;
}
months.push(“” + i1);
}
//获取日期
for (let i2 = date.getDate(); i2 <= 31; i2++) {
if (i2 < 10) {
i2 = “0” + i2;
}
days.push(“” + i2);
}
//获取小时
for (let i3 = date.getHours(); i3 < 24; i3++) {
if (i3 < 10) {
i3 = “0” + i3;
}
hours.push(“” + i3);
}

//监听picker的滚动事件
bindMultiPickerColumnChange: function(e) {
console.log(e);
//获取年份
if (e.detail.column == 0) {
let choose_year = this.data.multiArray[e.detail.column][e.detail.value];
if(e.detail.value==0){
that.setData({
[“multiArray[“+1+”]”]:months,
[“multiArray[“+2+”]”]:days,
[“multiArray[“+3+”]”]:hours,
choose_year:choose_year
})
}else{
this.setData({
choose_year:choose_year
});
console.log(“第一列滑动”);
let months = [];
let days = [];
let hours = [];
for (let i = 1; i <= 12; i++) {
if (i < 10) {
i = “0” + i;
}
months.push(“” + i);
}
for (let i1 = 1; i1 <= 31; i1++) {
if (i1 < 10) {
i1 = “0” + i1;
}
days.push(“” + i1);
}
for (let i2 = 7; i2 < 24; i2++) {
if (i2 < 10) {
i2 = “0” + i2;
}
hours.push(“” + i2);
}
console.log(months,days,hours);
that.setData({
[“multiArray[1]”]:months,
[“multiArray[2]”]:days,
[“multiArray[3]”]:hours
})
}
}
if (e.detail.column == 1) {
let choose_year = that.data.choose_year;
let choose_months = this.data.multiArray[e.detail.column][e.detail.value];
if(choose_year==date.getFullYear()){
if(e.detail.value==0){
that.setData({
[“multiArray[2]”]:days,
[‘multiArray[3]’]:hours,
choose_months:choose_months
})
}else{
let hours = [];
for (let i2 = 7; i2 < 24; i2++) {
if (i2 < 10) {
i2 = “0” + i2;
}
hours.push(“” + i2);
}
let num = parseInt(this.data.multiArray[e.detail.column][e.detail.value]);
let temp = [];
if (num == 1 || num == 3 || num == 5 || num == 7 || num == 8 || num == 10 || num == 12) { //判断31天的月份
for (let i = 1; i <= 31; i++) {
if (i < 10) {
i = “0” + i;
}
temp.push(“” + i);
}
} else if (num == 4 || num == 6 || num == 9 || num == 11) { //判断30天的月份
for (let i = 1; i <= 30; i++) {
if (i < 10) {
i = “0” + i;
}
temp.push(“” + i);
}
} else if (num == 2) { //判断2月份天数
let year = parseInt(this.data.choose_year);
console.log(year);
if (((year % 400 == 0) || (year % 100 != 0)) && (year % 4 == 0)) {
for (let i = 1; i <= 29; i++) {
if (i < 10) {
i = “0” + i;
}
temp.push(“” + i);
}
} else {
for (let i = 1; i <= 28; i++) {
if (i < 10) {
i = “0” + i;
}
temp.push(“” + i);
}
}
}
this.setData({
[‘multiArray[2]’]: temp,
[‘multiArray[3]’]: hours,
choose_months:choose_months
});
}
}else{
let num = parseInt(this.data.multiArray[e.detail.column][e.detail.value]);
let temp = [];
if (num == 1 || num == 3 || num == 5 || num == 7 || num == 8 || num == 10 || num == 12) { //判断31天的月份
for (let i = 1; i <= 31; i++) {
if (i < 10) {
i = “0” + i;
}
temp.push(“” + i);
}
} else if (num == 4 || num == 6 || num == 9 || num == 11) { //判断30天的月份
for (let i = 1; i <= 30; i++) {
if (i < 10) {
i = “0” + i;
}
temp.push(“” + i);
}
} else if (num == 2) { //判断2月份天数
let year = parseInt(this.data.choose_year);
console.log(year);
if (((year % 400 == 0) || (year % 100 != 0)) && (year % 4 == 0)) {
for (let i = 1; i <= 29; i++) {
if (i < 10) {
i = “0” + i;
}
temp.push(“” + i);
}
} else {
for (let i = 1; i <= 28; i++) {
if (i < 10) {
i = “0” + i;
}
temp.push(“” + i);
}
}
}
this.setData({
[‘multiArray[2]’]: temp,
[‘multiArray[3]’]: hours,
choose_months:choose_months
});
}
}
if (e.detail.column == 2) {
console.log(“riqi”);
let choose_year = that.data.choose_year;
let choose_months = that.data.choose_months;
if(choose_year==date.getFullYear() || choose_months==date.getMonth()+1){
if(e.detail.value==0){
that.setData({
[‘multiArray[3]’]:hours
})
}else{
let hours = [];
for (let i2 = 7; i2 < 24; i2++) {
if (i2 < 10) {
i2 = “0” + i2;
}
hours.push(“” + i2);
}
that.setData({
[‘multiArray[3]’]:hours
})
}
}
}
var data = {
multiArray: this.data.multiArray,
multiIndex: this.data.multiIndex
};
data.multiIndex[e.detail.column] = e.detail.value;
this.setData(data);
},

发表评论

评论列表

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~