vue3:获取1~12月份的第一天和最后一天

1、在utils目录下新键一个.js文件,在该文件中写入方法

// 获取当月的第一天和最后一天
export function getMonthFirstAndLastDay(CurrentMonth) {
    var now = new Date();
    var strLink = "-";
    var year = now.getFullYear();
    var month = CurrentMonth;
    var day = now.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (day >= 1 && day <= 9) {
      day = "0" + day;
    }
    var firstDate = year + strLink + month + strLink + '01';
    var sysDate = year + strLink + month + strLink + day;
    var lastDay = getLastDay(year, month);
    var lastDate = year + strLink + month + strLink + lastDay;
    var returnArr = [firstDate,  lastDate];//以数组形式返回
    return returnArr;
}
function getLastDay(year,month){
  var new_year = year;
  var new_month = month++;//取下一个月的第一天,方便计算(最后一天不固定)
  if(month>12){//如果当前大于12月,则年份转到下一年
    new_month -=12;//月份减
    new_year++;//年份增
  }
  // 取当年当月对应的下个月的前一天,即当前月的最后一天
  var last_date = new Date(new_year,new_month,0).getDate();
  return last_date;
}

2、在外部导入使用

<template>
<el-row>
     <el-button 
        style="width:80px"
        v-for="(item,index) in monthlist" 
        @click="handleClickMonths(index)" 
        :key="index">
        {{item}}
    </el-button>
</el-row>

</template>





<script setup>

import {getMonthFirstAndLastDay} from "@/utils/time.js";

const monthlist = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'];

const handleClickMonths=(indexs)=>{
  let getDates=getMonthFirstAndLastDay(Number(indexs)+1)
  console.log("getDates",getDates)

}

</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值