currentPlanChanged:async function(e){ let i = e.detail.value let plans = this.data.prices.plans let plan = plans[i] if(plan.name==this.data.plan)return let res = await wx.showModal({ cancelColor: 'cancelColor', title:'是否修改?', content:'修改当前的会员套餐' }) if(res.confirm){}else return console.log('时间打印:',this.data.realToday) let buyDate = this.data.expireDate > this.data.realToday ? this.data.expireDate : this.data.realToday let expireDate = lib.getDateByDeltaMonth(buyDate, plan.month) this.renderData({ plan:plan.name, buyDate, expireDate }) }这个函数无法正常设置日期,会显示错误,什么原因?这是另外一个函数function getDateByDeltaMonth(startDate, n) { var date = new Date(startDate) var m = date.getMonth() date.setMonth(m + n) var y = date.getFullYear() m = date.getMonth() + 1 var d = date.getDate() return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d) + '' }
时间: 2024-01-21 09:17:44 浏览: 75
Vue 报错TypeError: this.$set is not a function 的解决方法
5星 · 资源好评率100%
这段代码中,`lib.getDateByDeltaMonth` 函数的参数 `startDate` 和 `buyDate` 都是字符串类型,需要转换成 Date 类型才能进行日期计算。你可以在函数内部通过 `new Date(startDate)` 和 `new Date(buyDate)` 进行转换。
修改后的代码如下:
```
function getDateByDeltaMonth(startDate, n) {
var date = new Date(startDate)
var m = date.getMonth()
date.setMonth(m + n)
var y = date.getFullYear()
m = date.getMonth() + 1
var d = date.getDate()
return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d) + ''
}
async function currentPlanChanged(e) {
let i = e.detail.value
let plans = this.data.prices.plans
let plan = plans[i]
if (plan.name == this.data.plan) return
let res = await wx.showModal({
cancelColor: 'cancelColor',
title: '是否修改?',
content: '修改当前的会员套餐'
})
if (res.confirm) {
let buyDate = new Date(this.data.expireDate) > new Date(this.data.realToday) ? this.data.expireDate : this.data.realToday
let expireDate = getDateByDeltaMonth(buyDate, plan.month)
this.renderData({
plan: plan.name,
buyDate,
expireDate
})
} else {
return console.log('时间打印:', this.data.realToday)
}
}
```
阅读全文