原文链接

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. SpringBoot03:首页国际化

    页面国际化 有的时候,我们的网站会去涉及中英文甚至多语言的切换,这时候我们就需要学习国际化! 1.配置文件编写 首先在resources资源文件下新建一个i18n目录,存放国际化配置文件 新建一个lo ...

  2. [转帖]AES算法(五)GCM工作模式

    https://zhuanlan.zhihu.com/p/376692295 在以前介绍的基本工作模式中,ECB.CFB.OFB 三种模式可以解决 ECB 模式中相同明文生成相同密文的缺陷,CTR 又 ...

  3. [转帖]Jmeter脚本录:抓取https请求

    Jmeter抓取http请求 https://blog.csdn.net/qq19970496/article/details/86595109 代理设置步骤请参照该篇文章.本文件只做补充HTTPS中 ...

  4. [转帖]一文搞懂各种数据库SQL执行计划:MySQL、Oracle等

    https://zhuanlan.zhihu.com/p/99331255 MySQL 执行计划 Oracle 执行计划 SQL Server 执行计划 PostgreSQL 执行计划 执行计划(ex ...

  5. [转帖]⭐万字长篇超详细的图解Tomcat中间件方方面面储备知识⭐

    https://developer.aliyun.com/article/885079?spm=a2c6h.24874632.expert-profile.321.7c46cfe9h5DxWK 202 ...

  6. Python设计模式:你的代码真的够优雅吗?

    当涉及到代码优化时,Python作为一种高级编程语言,具有广泛的应用领域和强大的功能.在软件开发中,设计模式是一种被广泛采用的解决问题的方案,它提供了一种在特定情境中重复使用的可行方案.在Python ...

  7. 2022 倒带 - NutUI

    作者:京东零售 于明明 前言 时光飞逝,流年似水,让我们倒带 2022,回首这跌宕起伏一年走过的 "升级之路". NutUI 表现如何? 成绩单等着您打分! 2022 是 NutU ...

  8. vue启动报错_interopRequireDefault is not a function

    起因 今天接触一个项目vue. 在安装好环境之后,启动的时候报错_interopRequireDefault is not a function 解决的办法:我觉得可能是因为node_modules安 ...

  9. es6新增的运算符-链判断运算符的诞生[?.]和null的判断运算符??

    指数运算符 ** console.log(2 ** 2 ) //4 console.log(2 ** 3 ) //8 console.log(2 ** 4) //16 链判断运算符的诞生(?.) 在实 ...

  10. 01 vue子组件调用父组件中的方法

    vue子组件,调用父组件中有三种方法哈!下面我们一起来讲解. 第一种使用 直接在子组件中通过this.$parent.父组件中的方法.来调用父组件的方法 第一种的缺点是不能够传递参数哈.它只能够调用方 ...