moment时间计算函数

在数据计算中,运用到Array数组处理模块lodash,不熟悉的👬,请移步https://www.lodashjs.com/查阅

通过npm下载依赖

npm i --save lodash

在文件中引入

import _ from 'lodash'

本文用到的lodash解说

_.range([start=0], end, [step=1])


创建一个包含从 start 到 end,但不包含 end 本身范围数字的数组。 如果 start 是负数,而 end 或 step 没有指定,那么 step 从 -1 为开始。 如果 end 没有指定,start 设置为 0。 如果 end 小于 start ,会创建一个空数组,除非指定了 step。
注意:: JavaScript 遵循 IEEE-754 标准处理无法预料的浮点数结果

参数
[start=0] (number): 开始的范围。
end (number):       结束的范围。
[step=1] (number):  范围的增量 或者 减量。

返回
(Array): 返回范围内数字组成的新数组。

_.range(4);
// => [0, 1, 2, 3]
 
_.range(-4);
// => [0, -1, -2, -3]
 
_.range(1, 5);
// => [1, 2, 3, 4]
 
_.range(0, 20, 5);
// => [0, 5, 10, 15]
 
_.range(0, -4, -1);
// => [0, -1, -2, -3]
 
_.range(1, 4, 0);
// => [1, 1, 1]
 
_.range(0);
// => []

函数一
将数组 [2020,11] 转化为字符串 ‘2020年11月 ’

getTimeFormat(202011)
getTimeFormat(data){
      console.log(data);// 打印结果:[2020,11]
      const selected = moment().year(data[0]).month(data[1] - 1).startOf('month')
      const format = selected.format('YYYY年MM月')
      const start = +selected
      const end = +(selected.endOf('month'))
      console.log(format, start, end); //打印结果:2020年11月 1604160000000 1606751999999
}

函数二
求一段数据的汇总

const res = [getYears(1990, 2020)]
console.log(res)//打印结果如下图
function getYears(min, max) {
  return _.range(min, max + 1).map((year) => ({
    label: year + '年',
    value: year,
  }))
}

打印结果
在这里插入图片描述
函数三
输入一个年份,求季度

const res = getSeasons(2020)
console.log(res)//打印结果如下图
function getSeasons(year) {
  const max =
    year === moment().year() ? Math.floor(moment().month() / 3) + 1 : 4;
  return _.range(1, max + 1).map(season => ({
    label: season + '季度',
    value: season
  }));
}

在这里插入图片描述

函数四
输入一个年份,求月份

const res = getMonths(2020)
console.log(res)//打印结果如下图
function getMonths(year) {
  const max = year === moment().year() ? moment().month() + 1 : 12
  return _.range(1, max + 1).map(month => ({
    label: month + '月',
    value: month
  }))
}

在这里插入图片描述
函数5 输入年份求周

function getWeeks(year: number) {
  const result = []
  const today = moment()

  let current = moment(year, 'YYYY')
    .startOf('year')
    .startOf('week')
    .add(1, 'days')
  while (current.year() <= year && current.isBefore(today)) {
    const value = current.format('YYYY-MM-DD')
    const label =
      current.format('MM月DD日') +
      '-' +
      current.add(6, 'days').format('MM月DD日')
    result.push({
      value,
      label,
    })
    current = current.add(1, 'days')
  }
  console.log('week', result)//打印结果如下

  return result
}

在这里插入图片描述

const result = [getYears(currentTime.year() - 30, currentTime.year())]
console.log(result)//如图1
result.push(getWeeks(this.year))
console.log(result)//如图2

在这里插入图片描述
(图1)
在这里插入图片描述
在这里插入图片描述
(图2)
computed计算时间

computed: {
    defaultPickerValue() {//计算默认显示时间
      const current = moment(this.timeValue, 'YYYY年MM月');
      return [current.year(), current.month() + 1];
    },
    timeOptions() {//计算弹出窗显示的数据
      const currentTime = moment();
      const result = [getYears(currentTime.year() - 30, currentTime.year())];
      result.push(getMonths(this.year));
      return result;
    },
  }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值