element时间组件默认展示上月和本月

关键属性:default-value

当前6月显示5和6月
在这里插入图片描述

话不多说,上代码:

<template>
	<div>
      <el-date-picker  
          v-model="value"
          :key="value"
          :default-value="getBeforeMonth"
          size='small'
          type="daterange" >
      </el-date-picker>
	</div>	
</template>
<script>
  import { formatDate,dateIntervalToMonthStringArray } from '@/util/util'
  import func from "@/util/func";
  export default {
    name: 'searchInput',
    data() {
      return {
        getBeforeMonth: new Date(new Date().getFullYear(), new Date().getMonth() - 1)
    }
  }
</script>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用 JavaScript 中的 Date 对象来获取上个月时间范围。具体实现方法如下: ```javascript function getLastMonthRange() { const today = new Date(); const year = today.getFullYear(); const month = today.getMonth(); const lastMonth = month === 0 ? 12 : month; // 如果当前是1,则上个月是12 const lastMonthYear = month === 0 ? year - 1 : year; // 如果当前是1,则上个月的年份要减1 const firstDay = new Date(lastMonthYear, lastMonth - 1, 1); // 上个月的第一天 const lastDay = new Date(year, month, 0); // 当的最后一天 return { from: firstDay, to: lastDay, }; } ``` 这个函数返回一个包含上个月第一天和当最后一天的对象。你可以在 Vue 的组件中调用这个函数,然后将结果保存到组件的数据中,在模板中使用即可。例如: ```vue <template> <div> <p>上个月时间范围:{{ fromDate }} ~ {{ toDate }}</p> </div> </template> <script> export default { data() { return { fromDate: '', toDate: '', }; }, mounted() { const range = getLastMonthRange(); this.fromDate = range.from.toLocaleDateString(); this.toDate = range.to.toLocaleDateString(); }, methods: { getLastMonthRange() { // 实现参考上面的代码 }, }, }; </script> ``` 在上面的代码中,mounted 钩子函数会在组件挂载后自动调用,然后调用 `getLastMonthRange` 函数获取上个月时间范围,并将结果保存到组件的数据中。模板中使用 `{{ fromDate }}` 和 `{{ toDate }}` 显示这两个日期即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.T's Blog

感谢打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值