原文链接

https://cslaoxu.vip/110.html

说明

最近需要用到统计不同时间段内的记录数,所以找了一下现成的工具类。下面就演示一下如何引用到实际项目中。

详细用法请参考:https://github.com/Rattenking/GetPeriod

创建工具类

以我的项目为例,在utils文件夹下新建js文件:getperiod.js

class GetPeriod {
constructor() {
this.now = new Date();
this.nowYear = this.now.getYear(); //当前年
this.nowMonth = this.now.getMonth(); //当前月
this.nowDay = this.now.getDate(); //当前日
this.nowDayOfWeek = this.now.getDay(); //今天是本周的第几天
this.nowYear += (this.nowYear < 2000) ? 1900 : 0;
}
//格式化数字
formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
}
//格式化日期
formatDate(date) {
let myyear = date.getFullYear();
let mymonth = date.getMonth() + 1;
let myweekday = date.getDate();
return [myyear, mymonth, myweekday].map(this.formatNumber).join('/');
}
//获取某月的天数
getMonthDays(myMonth) {
let monthStartDate = new Date(this.nowYear, myMonth, 1);
let monthEndDate = new Date(this.nowYear, myMonth + 1, 1);
let days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
return days;
}
//获取本季度的开始月份
getQuarterStartMonth() {
let startMonth = 0;
if (this.nowMonth < 3) {
startMonth = 0;
}
if (2 < this.nowMonth && this.nowMonth < 6) {
startMonth = 3;
}
if (5 < this.nowMonth && this.nowMonth < 9) {
startMonth = 6;
}
if (this.nowMonth > 8) {
startMonth = 9;
}
return startMonth;
}
//获取今天的日期
getNowDate() {
return this.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay));
}
//获取本周的开始日期
getWeekStartDate() {
return this.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 1));
}
//获取本周的结束日期
getWeekEndDate() {
return this.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay + (6 - this.nowDayOfWeek + 1)));
}
//获取本月的开始日期
getMonthStartDate() {
return this.formatDate(new Date(this.nowYear, this.nowMonth, 1));
}
//获取本月的结束日期
getMonthEndDate() {
return this.formatDate(new Date(this.nowYear, this.nowMonth, this.getMonthDays(this.nowMonth)));
}
//获取本季度的开始日期
getQuarterStartDate() {
return this.formatDate(new Date(this.nowYear, this.getQuarterStartMonth(), 1));
}
//获取本季度的结束日期
getQuarterEndDate() {
return this.formatDate(new Date(this.nowYear, this.getQuarterStartMonth() + 2, this.getMonthDays(this.getQuarterStartMonth() + 2)));
}
//获取本年的开始日期
getYearStartDate() {
return this.formatDate(new Date(this.nowYear, 0, 1));
}
//获取本年的结束日期
getYearEndDate() {
return this.formatDate(new Date(this.nowYear, 11, 31));
}
//获取时段方法
getPeriod(obj) {
let opts = obj || {}, time = null;
opts = {
periodType: opts.periodType || 'now',
spaceType: opts.spaceType || '~'
}
function formatNumber(param1, param2) {
return [param1, param2].join(opts.spaceType);
}
if (opts.periodType == 'week') {
time = formatNumber(this.getWeekStartDate(), this.getWeekEndDate());
} else if (opts.periodType == 'month') {
time = formatNumber(this.getMonthStartDate(), this.getMonthEndDate());
} else if (opts.periodType == 'quarter') {
time = formatNumber(this.getQuarterStartDate(), this.getQuarterEndDate());
} else if (opts.periodType == 'year') {
time = formatNumber(this.getYearStartDate(), this.getYearEndDate());
} else {
time = formatNumber(this.getNowDate(), this.getNowDate());
}
return time;
}
}
module.exports = GetPeriod;

引入js

  • 在页面index.js开头引入
const GetPeriod = require("../../utils/getperiod.js");
  • data结构里面声明一个对象变量
data: {
period: ''
}
  • 在页面加载时创建对象实例
    onLoad函数开头:
this.data.period = new GetPeriod();

调用方法

在需要的地方调用其方法,例如:

// 本周
if (this.data.period_index == 1) {
startDate = this.data.period.getWeekStartDate();
endDate = this.data.period.getWeekEndDate();
}

微信小程序获取本日、本周、本月、本年时间段的更多相关文章

  1. 微信小程序-获取经纬度

    微信小程序-获取经纬度 最近公司新功能 要求在外的市场人员 发送位置信息回来. 用的还是微信小程序开发.... 微信小程序 提供一个接口 getLocation 这个接口反回来的位置 相对实际位置 相 ...

  2. 微信小程序-获取当前城市位置及再次授权地理位置

    微信小程序-获取当前城市位置 1. 获取当前地理位置,可通过wx.getLocation接口,返回经纬度.速度等信息; 注意---它的默认工作机制: 首次进入页面,调用该api,返回用户授权结果,并保 ...

  3. 微信小程序获取Access_token和页面URL生成小程序码或二维码

    1.微信小程序获取Access_token: access_token具体时效看官方文档. using System; using System.Collections.Generic; using ...

  4. [微信小程序] 微信小程序获取用户定位信息并加载对应城市信息,wx.getLocation,腾讯地图小程序api,微信小程序经纬度逆解析地理信息

    因为需要在小程序加个定位并加载对应城市信息 然而小程序自带api目前只能获取经纬度不能逆解析,虽然自己解析方式,但是同时也要调用地图,难道用户每次进小程序还要强行打开地图选择地址才定位吗?多麻烦也不利 ...

  5. C# 微信小程序获取openid sessionkey

    项目介绍 1.微信小程序获取openid和session_key 2.后台使用C#开发 项目流程 准备工作 1 获取appid 1.1 下载微信web开发工具 https://developers.w ...

  6. JavaScript和微信小程序获取IP地址的方法

    最近公司新加了一个需求,根据用户登录的IP地址判断是否重复登录,重复登录就进行逼退,那么怎么获取到浏览器的IP地址呢?最后发现搜狐提供了一个JS接口,可以通过它获取到客户端的IP. 接口地址如下: h ...

  7. 微信小程序获取输入框(input)内容

    微信小程序---获取输入框(input)内容 wxml <input placeholder="请输入手机号码" maxlength="11" type= ...

  8. .Net之微信小程序获取用户UnionID

    前言: 在实际项目开发中我们经常会遇到账号统一的问题,如何在不同端或者是不同的登录方式下保证同一个会员或者用户账号唯一(便于用户信息的管理).这段时间就有一个这样的需求,之前有个客户做了一个微信小程序 ...

  9. 微信小程序获取手机号码看这篇文章就够了

    前言 微信小程序获取手机号码,从官方文档到其他博主的文档 零零散散的 (我就是这样看过来 没有一篇满意的 也许是我搜索姿势不对) 依旧是前人栽树 后人乘凉 系列.保证看完 就可以实现获取手机号码功能 ...

  10. 图解微信小程序---获取电影信息

    图解微信小程序---获取电影信息 代码笔记 第一步:编写js文件,调用api获取相对应电影详情信息(注意带入的参数是id不在是榜单的type,电影api的movie后面又斜杠,别忘了,对应的绑定数据的 ...

随机推荐

  1. [转帖]5 分钟学会写一个自己的 Prometheus Exporter

    https://cloud.tencent.com/developer/article/1520621学习一下怎么搭建呢.   去年底我写了一个阿里云云监控的 Prometheus Exporter, ...

  2. [转帖]ARM内核全解析,从ARM7,ARM9到Cortex-A7,A8,A9,A12,A15到Cortex-A53,A57

    https://www.cnblogs.com/senior-engineer/p/8668723.html 前不久ARM正式宣布推出新款ARMv8架构的Cortex-A50处理器系列产品,以此来扩大 ...

  3. [转帖]Kafka之ack机制

    前言 之前的博客里说了,Kafka的消息同步是一种ISR机制,本质上是"完全同步"的一种优化. 都在说,消息被ISR中所有副本都写入才算写入成功.但是这样未免定的太死板了,所以,K ...

  4. [转帖]Kafka之ISR机制的理解

    Kafka对于producer发来的消息怎么保证可靠性? 每个partition都给配上副本,做数据同步,保证数据不丢失. 副本数据同步策略 和zookeeper不同的是,Kafka选择的是全部完成同 ...

  5. 【转帖】Windows Server 2016与旧版本系统比较

    一.性能和可扩性 特征描述 Windows Server 2012/2012 R2 标准版和数据中心 Windows Server 2016 标准版和数据中心 物理内存(主机)支持 每个物理服务器至多 ...

  6. Python学习之五_字符串处理生成查询SQL

    Python学习之五_字符串处理生成查询SQL 前言 昨天想给同事讲解一下获取查询部分表核心列信息的SQL方法 也写好了一个简单文档. 但是感觉不是很优雅. 最近两三天晚上一直在学习Python. 想 ...

  7. [转帖]Springboot容器化镜像设置堆内存大小

    参考资料:Best Practices: Java Memory Arguments for Containers - DZone Java 有三种方式设置最大堆内存大小: 1. -Xmx 2. -X ...

  8. MySQL查询聚合函数与分组查询

    连接数据库 mysql -hlocalhost -uroot -proot 聚合函数 聚合函数:作用于某一列,对数据进行计算. ps: 所有的null值是不参与聚合函数的运算的. 06 常见的聚合函数 ...

  9. vue中diff算法处理新旧节点的流程

    vue中diff算法处理新旧节点的流程 patch函数的作用 function patch(oldVnode: VNode | Element, vnode: VNode): VNode { let ...

  10. 关于async函数的错误处理

    1. 关于async函数的错误处理 有些时候,我们请求的接口可能会报错: 从而导致后面的代码无法去执行: 这样就会造成页面上某些状态出错! 那么怎么样才能 既能捕获到错误 还能让代码往后面执行呢 2. ...