Js implementation of small program date switch (last day, next day)
Data structure
Data: {date: 'today ', targetNum: 0, nextNum: 0},Copy the code
Encapsulate the date handler function
Showdate (n) {return time format YYYY-MM-DD var date = new date(); date.setDate(date.getDate() + n); var month = date.getMonth() + 1 month = month > 10 ? Month: '0' + month // format month var day = date.getDate() day = day > 10? Date = date.getFullYear() + "-" + month + "-" + day return date; },Copy the code
Encapsulates a function to switch dates
Num: yesterday -1, the day before yesterday -2, tomorrow 1, the day after tomorrow 2, the day after tomorrow 1, the day after tomorrow 2, the day after tomorrow 5, 0, today
Gargetdata (num){var that = this var date = this.showdate(num) date = date == this.showdate(0)? Date = date == this.showdate(-1)? 'yesterday' : date date = date == this.showdate(1)? SetData ({date: date, targetNum: num})},Copy the code
Num -1 is triggered by the button click event of the previous day
UpDate (){var that = this var num = thate.data.targetnum - 1 that. Gargetdata (num)},Copy the code
The next day button click event triggers a num+1
NextDate (){var that = this if (thate.data.date == 'today ')return var num = thate.data.targetnum + 1 that.gargetdata(num) },Copy the code
Supplementary: time difference calculation
getDays(timestr) { var rDate = {}; Var finallyDate = new Date(parseInt(dateStrs[0], 10), (parseInt(dateStrs[1], 10) - 1), parseInt(dateStrs[2], 10) + 1); var NowDate = new Date(); Var Dtime = finallyDate - NowDate; Days = math.floor (Dtime/(24 * 3600 * 1000)); rdate.days = math.floor (Dtime/(24 * 3600 * 1000)); return rDate; },Copy the code
If it helps you, please give a “like” before you leave. Thank you
Follow me and share technical dry goods from time to time