<el-form-item label="日期:">{{dateRange(this.begin,this.end)}}  ({{this.begin}}至{{this.end}})</el-form-item>
dateRange (t, t1) {
  let range = ''
  let sj = new Date(t)
  let sj1 = new Date(t1)
  let y = sj.getFullYear()
  let m = sj.getMonth() + 1
  let d = sj.getDate()
  let y1 = sj1.getFullYear()
  let m1 = sj1.getMonth() + 1
  let d1 = sj1.getDate()
  let month = Math.abs((y1 - y) * 12 + (m1 - m))
  // 扩展,计算两个日期相差多少年多少月多少天
  let y2 = Math.floor(month / 12)
  // 天数:如果d1<d,d1=30+d1,然后mont-1.如果d1>d,就直接d1-d
  let mont = month % 12
  let day2 = Math.abs(d1 - d)
  if (d1 < d) {
    d1 = d1 + 30
    day2 = Math.abs(d1 - d)
    mont = mont - 1
  }
  if (y2 > 0) range += y2 + '年'
  if (mont > 0) range += mont + '月'
  if (day2 > 0) range += day2 + '天'
  return range
}
Logo

前往低代码交流专区

更多推荐