当前月份的第一天和最后一天

复制代码
<template>
  <div class='home-page'>内容</div>
</template>

<script >
import { defineComponent, onMounted } from 'vue'

export default defineComponent({
  setup () {
    function getCurrentMonthFirst () {
      var date = new Date()
      date.setDate(1)
      var month = parseInt(date.getMonth() + 1)
      console.log(month)
      var day = date.getDate()
      if (month < 10) {
        month = '0' + month
      }
      if (day < 10) {
        day = '0' + day
      }
      console.log(day)
      console.log(date.getFullYear() + '-' + month + '-' + day)
      return date.getFullYear() + '-' + month + '-' + day
    }

    function getCurrentMonthLast () {
      var date = new Date()
      var currentMonth = date.getMonth()
      console.log('currentMonth', currentMonth)
      var nextMonth = ++currentMonth
      console.log('currentMonth', nextMonth)
      var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1)
      console.log('nextMonthFirstDay', nextMonthFirstDay)
      var oneDay = 1000 * 60 * 60 * 24
      var lastTime = new Date(nextMonthFirstDay - oneDay)
      console.log(lastTime)
      var month = parseInt(lastTime.getMonth() + 1)
      var day = lastTime.getDate()
      if (month < 10) {
        month = '0' + month
      }
      if (day < 10) {
        day = '0' + day
      }
      console.log(date.getFullYear() + '-' + month + '-' + day)
      return date.getFullYear() + '-' + month + '-' + day
    }
    onMounted(() => {
      getCurrentMonthFirst()
      getCurrentMonthLast()
    })
    return { getCurrentMonthFirst, getCurrentMonthLast }
  }
})
</script>

<style lang='less'>
.home-page {
  height: 100px;
  border: 1px solid red;
}
</style>
复制代码

 

posted @   zjxgdq  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示