elementUI日期选择器快速选择(今天,近七天,近一个月,上个月,这个月)

关于(今天,近七天,近一个月,上个月,这个月)快捷选择

  • 最近做了一个基于vue-element-admin开源框架的后台项目,其中有块数据分析的模块,有个日期快捷选择的需求(主要里面竟然有上个月、这个月这种奇葩日期),咱也不能要求产品经理修改需求,咱做的就是想办法实现他,不多费口舌,上代码。
<template>
  <div class="dashboard-editor-container">
	<el-date-picker
          v-model="dataTime"
          type="daterange"
          align="right"
          size="small"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions2"
          @change="dataTimeChange"
        />
	</div>
</template>
export default { 
	data() { 
		return { 
			pickerOptions2: { 
        // 今天,昨天,这个月,上个月
        shortcuts: [
          { 
            text: '今天',
            onClick(picker) { 
              const end = new Date()
              const start = new Date()
              picker.$emit('pick', [start, end])
            }
          },
          { 
            text: '昨天',
            onClick(picker) { 
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 1)
              picker.$emit('pick', [start, end])
            }
          },
          { 
            text: '最近一周',
            onClick(picker) { 
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          { 
            text: '最近一个月',
            onClick(picker) { 
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          { 
            text: '这个月',
            onClick(picker) { 
              const end = getCurrentMonthLast()
              const start = getCurrentMonthFirst()
              picker.$emit('pick', [start, end])
              function getCurrentMonthFirst() { 
                var date = new Date()
                console.log(date)
                date.setDate(1)
                return date
              }
              // 获取当前月的最后一天
              function getCurrentMonthLast() { 
                var date = new Date()
                var currentMonth = date.getMonth()
                var nextMonth = ++currentMonth
                var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1)
                var oneDay = 1000 * 60 * 60 * 24
                return new Date(nextMonthFirstDay - oneDay)
              }
            }
          },
          { 
            text: '上个月',
            onClick(picker) { 
              const end = gettimeEnd()
              const start = gettimeStart()
              picker.$emit('pick', [start, end])
              function gettimeStart() { 
                const nowdays = new Date()
                let year = nowdays.getFullYear()
                let month = nowdays.getMonth()
                if (month === 0) { 
                  month = 12
                  year = year - 1
                }
                if (month < 10) { 
                  month = '0' + month
                }
                let firstDayOfPreMonth = year + '-' + month + '-' + '01'
                firstDayOfPreMonth = firstDayOfPreMonth.toString()
                return new Date(firstDayOfPreMonth)
              }
              function gettimeEnd() { 
                const nowdays = new Date()
                let year = nowdays.getFullYear()
                let month = nowdays.getMonth()
                if (month === 0) { 
                  month = 12
                  year = year - 1
                }
                if (month < 10) { 
                  month = '0' + month
                }
                const lastDay = new Date(year, month, 0)
                let lastDayOfPreMonth = year + '-' + month + '-' + lastDay.getDate()
                lastDayOfPreMonth = lastDayOfPreMonth.toString()
                return new Date(lastDayOfPreMonth)
              }
            }
          }
        ]
      },
		}
	},
	methods: { 
		dataTimeChange(value) { 
			console.log(value)
		}
	}
}
    原文作者:前端菜菜子
    原文地址: https://blog.csdn.net/weixin_41552521/article/details/103889839
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞