小程序picker-view的日期范围设置

date.axml





























{{item}}





{{item}}





{{item}}











date.js

import util from './webUtils'

Component({

mixins: [],

data: {

year:[],

month:[1,2,3,4,5,6,7,8,9,10,11,12],

day:[],

selectYear:'',

selectMonth:'',

selectDay:'',

prickShow:true,

value:[1,1,1]

},

props: {

isnext:false,

contentheight: '20%',

showDate:true,

onClear:(data) => console.log(data),

onSure:(data) => console.log(data),

},

didMount() {

let value = [1]

let data = new Date()

if (this.props.isnext){

var timestamp = Date.parse(data);

console.info(timestamp)

data = new Date(timestamp+ 24*60*60*1000)

}

// 年

let year = []

let nowYear =Number(data.getFullYear())

for(let y=0;y<10;y++){

year.push(nowYear - 1 + y)

}

// year[0] = Number(data.getFullYear()) -1

// year[1] = Number(data.getFullYear())



// 月

let month = Number(data.getMonth())+1

// 当前月在数组的index

value[1] = this.data.month.indexOf(month)



// 这个月有多少天

let day = []

let maxDay = Number(util.mGetDate(year[1],month))

// 动态添加当前月的所有天数

for(let i=1;i<=maxDay;i++){

day.push(i)

}

// 今天在数组的index

value[2] = day.indexOf(data.getDate())

// for(){}

// let months;

// let m = data.getMonth()+1;

// if (m < 10){

// months = '0' + m

// }else{

// months = m

// }

let months = data.getMonth()+1;

this.setData({

year,

day,

value,

selectYear:data.getFullYear(),

selectMonth:months,

selectDay:data.getDate(),

})

},

didUpdate() {

},

didUnmount() {},



methods: {

clear(e){

// this.setData({

// showDate:false

// })

this.props.onClear(e);

},//#endregion

sure(e){

let data = new Date();

let year = Number(data.getFullYear());

let month = Number(data.getMonth())+1;

let day = Number(data.getDate())

let selectyear1 = this.data.selectYear

let selectmonth1 = this.data.selectMonth

let selectday1 = this.data.selectDay

//当选中的年份小于等于当前不能跳转

if(selectyear1 > year){

if (selectmonth1 < 10){

selectmonth1 = '0' + selectmonth1

}

if (selectday1 < 10){

selectday1 = '0' + selectday1

}

this.props.onSure(selectyear1+'-'+selectmonth1+'-'+selectday1);

}

//当选中的年份小于当前不能跳转

else if(selectyear1 = year){

if(selectmonth1 > month){

if (selectmonth1 < 10){

selectmonth1 = '0' + selectmonth1

}

if (selectday1 < 10){

selectday1 = '0' + selectday1

}

this.props.onSure(selectyear1+'-'+selectmonth1+'-'+selectday1);

}

else if(selectmonth1 = month){

if (selectmonth1 < 10){

selectmonth1 = '0' + selectmonth1

}

if(selectday1 >= day){

if (selectday1 < 10){

selectday1 = '0' + selectday1

}

this.props.onSure(selectyear1+'-'+selectmonth1+'-'+selectday1);

}

if(selectday1 < day){

selectday1 = day

if (selectday1 < 10){

selectday1 = '0' + selectday1

}

this.props.onSure(selectyear1+'-'+selectmonth1+'-'+selectday1);

}

}

}

},

onChange(e) {

  // y用户改变月份后 天数对着改变

let year = this.data.year[e.detail.value[0]]

let month = this.data.month[e.detail.value[1]]

let days = this.data.day[e.detail.value[2]]

// 这个月有多少天

let day = []

let maxDay = Number(util.mGetDate(year,month))

// 动态添加当前月的所有天数

for(let i=1;i<=maxDay;i++){

day.push(i)

}

this.setData({

value: e.detail.value,

day,

selectYear:year,

selectMonth:month,

selectDay:days,

});

console.info(e.detail.value)

},

},

});

小程序picker-view的日期范围设置_第1张图片

当点击“完成”按钮时判断选中的时间是否为当前日期及以后的。

你可能感兴趣的:(小程序,js)