暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

时间操作函数

Geeker工作坊 2021-08-22
753

时间操作函数

开始之前

近期接到一个数据统计的项目,里面要求对数据进行一些本周、本月、本年等数据展示和同比、环比、比上周、比上月、比上年对比分析。后台的接口要求传一个时间段才能获取相应的数据。接口参数如下:

    {
    "BeginTime": "2021-08-22T08:50:16.624Z",
    "EndTime": "2021-08-22T08:50:16.624Z",
    "EntityID": "string"
    }

    举个例子:

    如果要查询本周的数据,那么BeginTime为本周第一天日期,EndTime则为本周的最后一天。

    处理这种时间操作一种方法是JS时间库,另一个那就是自己手写一个了。

    对于第一种,常用的时间库有moment.js 和 dayjs. 两个功能相似,用法一致。两者dayjs体积比较小,所以更推荐,具体如何使用可以参考官网。(Day.js · 中文文档 - 2kB 大小的 JavaScript 时间日期库 (gitee.io)

    使用时间库再处理周的时候,每个周的第一天是周日,最后一天是周六,这个在开发的时候需要注意一下!

    手撕时间操作函数

    由于时间库操作时间相对容易,本文就不做过多介绍了,本文主要介绍如何自己实现一个能够满足本项目要求的时间操作函数,且要符合我国的习惯(周一是每个周的第一天,周日是最后一天)。废话不多说了我直接上代码了,大家直接看代码学习吧,如果问题随时可以交流。

      // 对Date的扩展,将 Date 转化为指定格式的String
      // 月(M)、日(d)、小时(H)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
      // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
      // 例子:
      // (new Date()).Format("yyyy-MM-dd HH:mm:ss.S") ==> 2006-07-02 08:09:04.423
      // (new Date()).Format("yyyy-M-d H:m:s.S") ==> 2006-7-2 8:9:4.18
      Date.prototype.Format = function(fmt) {
      let o = {
      "M+" : this.getMonth()+1, //月份
      "d+" : this.getDate(), //日
      "h+" : this.getHours(), //小时
      "m+" : this.getMinutes(), //分
      "s+" : this.getSeconds(), //秒
      "q+" : Math.floor((this.getMonth()+3)/3), //季度
      "S" : this.getMilliseconds() //毫秒
      };
      if(/(y+)/.test(fmt))
      fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
      for(let k in o)
      if(new RegExp("("+ k +")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
      return fmt;
      }


      /**
      * 获取本周、本季度、本月、上月的开始日期、结束日期
      */
      let now = new Date(); //当前日期
      let nowDayOfWeek = now.getDay() || 7; //今天本周的第几天
      let nowDay = now.getDate(); //当前日
      let nowMonth = now.getMonth(); //当前月
      let nowYear = now.getFullYear(); //当前年
      let lastMonthDate = new Date(); //上月日期
      lastMonthDate.setDate(1);
      lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
      let lastMonth = lastMonthDate.getMonth();


      //获得某月的天数
      export function getMonthDays(month) {
      if(Number.isInteger(month) && month) {
      let nowYear = new Date().getFullYear();
      let monthStartDate = new Date(nowYear, month - 1 , 1);
      let monthEndDate = new Date(nowYear, month, 1);
      return (monthEndDate - monthStartDate) (1000 * 60 * 60 * 24);
      } else {
      throw "请输入正整数"
      }
      }


      //获得本季度的开始月份
      function getQuarterStartMonth() {
      let quarterStartMonth = 0;
      if (nowMonth < 3) {
      quarterStartMonth = 0;
      }
      if (2 < nowMonth && nowMonth < 6) {
      quarterStartMonth = 3;
      }
      if (5 < nowMonth && nowMonth < 9) {
      quarterStartMonth = 6;
      }
      if (nowMonth > 8) {
      quarterStartMonth = 9;
      }
      return quarterStartMonth;
      }


      //获得本周的开始日期
      export function getWeekStartDate(fmt = "yyyy-MM-dd") {
      let weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);
      return weekStartDate.Format();
      }


      //获得本周的结束日期
      export function getWeekEndDate(fmt = "yyyy-MM-dd") {
      let weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek));
      return weekEndDate.Format(fmt);
      }


      //获得上周的开始日期
      export function getLastWeekStartDate(fmt = "yyyy-MM-dd") {
      let weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 6);
      return weekStartDate.Format(fmt);
      }


      //获得上周的结束日期
      export function getLastWeekEndDate(fmt = "yyyy-MM-dd") {
      let weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
      return weekEndDate.Format(fmt);
      }


      //获得本月的开始日期
      export function getMonthStartDate(fmt = "yyyy-MM-dd") {
      let monthStartDate = new Date(nowYear, nowMonth, 1);
      return monthStartDate.Format(fmt);
      }


      //获得本月的结束日期
      export function getMonthEndDate(fmt = "yyyy-MM-dd") {
      let monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth + 1));
      return monthEndDate.Format(fmt);
      }


      //获得上月开始时间
      export function getLastMonthStartDate(fmt = "yyyy-MM-dd") {
      let lastMonthStartDate = new Date(nowYear, lastMonth, 1);
      return lastMonthStartDate.Format(fmt);
      }


      //获得本季度的开始日期
      export function getQuarterStartDate(fmt = "yyyy-MM-dd") {
      let quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
      return quarterStartDate.Format(fmt);
      }


      //获得的本季度的结束日期
      export function getQuarterEndDate(fmt = "yyyy-MM-dd") {
      let quarterEndMonth = getQuarterStartMonth() + 2;
      let quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth + 1));
      return quarterStartDate.Format(fmt);
      }


      //获得上年开始时间
      export function getLastYearStartDate(fmt = "yyyy-MM-dd") {
      let lastYearStartDate = new Date(nowYear -1 , 0, 1);
      return lastYearStartDate.Format(fmt);
      }


      //获得上年结束时间
      export function getLastYearEndDate(fmt= "yyyy-MM-dd") {
      let lastYearEndDate = new Date(nowYear -1 , 11, 31);
      return lastYearEndDate.Format(fmt);
      }


      //获得今年开始时间
      export function getYearStartDate(fmt = "yyyy-MM-dd") {
      let yearStartDate = new Date(nowYear , 0, 1);
      return yearStartDate.Format(fmt);
      }


      //获得今年结束时间
      export function getYearEndDate(fmt= "yyyy-MM-dd") {
      let yearEndDate = new Date(nowYear, 11, 31);
      return yearEndDate.Format(fmt);
      }


      文章转载自Geeker工作坊,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

      评论