获取某年某月的天数

获取某年某月的天数

当前页面引入

<div class="top" @click="showStart = true">
      <div class="title">{
   
   { time == '' ? '全部日期' : time }}</div>
      <div class="top-image">
        <img alt="" src="../../assets/images/icon-down.png" />
      </div>
    </div>
     <van-popup v-model="showStart" position="bottom">
      <van-datetime-picker
        v-model="startTime"
        :formatter="formatter"
        :max-date="maxDate"
        :min-date="minDate"
        title="选择月份"
        type="year-month"
        @cancel="showStart = false"
        @confirm="onConfirmStart"
      />
    </van-popup>
  import { formatTime } from '@/utils'
mounted(){
 this.endTimes = this.getMonthDay(2023,07)
}
methods: {
 onConfirmStart(value) {
        let startTime = formatTime(value, '-')
        this.endTimes = this.getMonthDay(
          startTime.split('-')[0],
          startTime.split('-')[1]
        )
        this.time =
          startTime.split('-')[0] + '年' + startTime.split('-')[1] + '月'
      },
getMonthDay(year, month) {
        let day = 0
        month = parseInt(month)
        let bigMonth = [1, 3, 5, 7, 8, 10, 12]
        if (bigMonth.indexOf(month) != -1) {
          day = 31
        } else if (month == 2) {
          if (this.getYearDay(year) === 365) {
            day = 28
          }
          if (this.getYearDay(year) === 366) {
            day = 29
          }
        } else {
          day = 30
        }
        return day
      },
      getYearDay(year) {
        //var year = new Date().getFullYear()
        let day = 365
        if (year % 4 === 0) {
          day = 366
        }
        return day
      },
   }

在utils文件夹下的index.js中写入

// 当前时间的前14天-当前时间
export const isTime = (val) => {
  //   1.获取当前时间年月日时分秒格式xxxx-xx-xx xx:xx:xx
  var myDate = new Date() // 当前时间
  var y = myDate.getFullYear() // 当前年份四位数
  var m =
    myDate.getMonth() + 1 < 10
      ? '0' + (myDate.getMonth() + 1)
      : myDate.getMonth() + 1 // 月份为0-11,在10月前更改为01格式
  var d = myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate() // 日期
  var h = myDate.getHours() < 10 ? '0' + myDate.getHours() : myDate.getHours() // 小时
  var mi =
    myDate.getMinutes() < 10 ? '0' + myDate.getMinutes() : myDate.getMinutes() // 分钟
  var s =
    myDate.getSeconds() < 10 ? '0' + myDate.getSeconds() : myDate.getSeconds() // 秒
  var end = y + '-' + m + '-' + d + ' ' + h + ':' + mi + ':' + s // 格式

  // 对十四天前的日期进行获取
  myDate.setDate(myDate.getDate() - 14) // 获取十四天前的时间
  y = myDate.getFullYear()
  m =
    myDate.getMonth() + 1 < 10
      ? '0' + (myDate.getMonth() + 1)
      : myDate.getMonth() + 1
  d = myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate()
  h = myDate.getHours() < 10 ? '0' + myDate.getHours() : myDate.getHours()
  mi =
    myDate.getMinutes() < 10 ? '0' + myDate.getMinutes() : myDate.getMinutes()
  s = myDate.getSeconds() < 10 ? '0' + myDate.getSeconds() : myDate.getSeconds()
  var start = y + '-' + m + '-' + d + ' ' + h + ':' + mi + ':' + s
  val = start + '-' + end
  return val
}
// 当前时间的前N天-当前时间
export const TimeN = (val) => {
  //   1.获取当前时间年月日时分秒格式xxxx-xx-xx xx:xx:xx
  var myDate = new Date() // 当前时间
  var y = myDate.getFullYear() // 当前年份四位数
  var m =
    myDate.getMonth() + 1 < 10
      ? '0' + (myDate.getMonth() + 1)
      : myDate.getMonth() + 1 // 月份为0-11,在10月前更改为01格式
  var d = myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate() // 日期
  // var h = myDate.getHours() < 10 ? '0' + myDate.getHours() : myDate.getHours() // 小时
  // var mi =
  //   myDate.getMinutes() < 10 ? '0' + myDate.getMinutes() : myDate.getMinutes() // 分钟
  // var s =
  //   myDate.getSeconds() < 10 ? '0' + myDate.getSeconds() : myDate.getSeconds() // 秒
  var end = y + '-' + m + '-' + d + ' ' + '23' + ':' + '59' + ':' + '59' // 格式

  // 对N天前的日期进行获取
  myDate.setDate(myDate.getDate() - val) // 获取N天前的时间
  y = myDate.getFullYear()
  m =
    myDate.getMonth() + 1 < 10
      ? '0' + (myDate.getMonth() + 1)
      : myDate.getMonth() + 1
  d = myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate()
  // h = myDate.getHours() < 10 ? '0' + myDate.getHours() : myDate.getHours()
  // mi =
  //   myDate.getMinutes() < 10 ? '0' + myDate.getMinutes() : myDate.getMinutes()
  // s = myDate.getSeconds() < 10 ? '0' + myDate.getSeconds() : myDate.getSeconds()
  var start = y + '-' + m + '-' + d + ' ' + '00' + ':' + '00' + ':' + '00'
  let time = start + '/' + end
  return time
}
// 当前时间
export function getFirst() {
  // var date = new Date()
  // date.setDate(1)
  // var cTime = new Date(new Date().toLocaleDateString()).getTime()
  //将时间戳转换成时间格式
  // var date1 = new Date(cTime)
  var date1 = new Date()
  var year = date1.getFullYear()
  var month = date1.getMonth() + 1
  var day = date1.getDate()
  var hh = date1.getHours() // 小时
  var minu = date1.getMinutes() // 分钟
  var scn = date1.getSeconds() // 秒
  month = month < 10 ? '0' + month : month
  day = day < 10 ? '0' + day : day
  hh = hh < 10 ? '0' + hh : hh
  minu = minu < 10 ? '0' + minu : minu
  scn = scn < 10 ? '0' + scn : scn
  var ddate = year + '-' + month + '-' + day + ' ' + hh + ':' + minu + ':' + scn
  return ddate
}
// 选择年月
export const formatTime = (date, type) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  console.log(date.getDate)

  // const hour = date.getHours()
  // const minute = date.getMinutes()
  // const second = date.getSeconds()

  const t1 = [year, month].map(formatNumber).join(type)
  // const t2 = [hour, minute, second].map(formatNumber).join(":");
  console.log(t1)
  return `${t1}`
}
// 选择年月日
export const formatTimeDay = (date, type) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  console.log(date.getDate)

  // const hour = date.getHours()
  // const minute = date.getMinutes()
  // const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join(type)
  // const t2 = [hour, minute, second].map(formatNumber).join(":");
  console.log(t1)
  return `${t1}`
}

猜你喜欢

转载自blog.csdn.net/q4717529/article/details/131982937