微信小程序获取本日、本周、本月、本年时间段
原文链接
说明
最近需要用到统计不同时间段内的记录数,所以找了一下现成的工具类。下面就演示一下如何引用到实际项目中。
详细用法请参考: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();
}
微信小程序获取本日、本周、本月、本年时间段的更多相关文章
- 微信小程序-获取经纬度
微信小程序-获取经纬度 最近公司新功能 要求在外的市场人员 发送位置信息回来. 用的还是微信小程序开发.... 微信小程序 提供一个接口 getLocation 这个接口反回来的位置 相对实际位置 相 ...
- 微信小程序-获取当前城市位置及再次授权地理位置
微信小程序-获取当前城市位置 1. 获取当前地理位置,可通过wx.getLocation接口,返回经纬度.速度等信息; 注意---它的默认工作机制: 首次进入页面,调用该api,返回用户授权结果,并保 ...
- 微信小程序获取Access_token和页面URL生成小程序码或二维码
1.微信小程序获取Access_token: access_token具体时效看官方文档. using System; using System.Collections.Generic; using ...
- [微信小程序] 微信小程序获取用户定位信息并加载对应城市信息,wx.getLocation,腾讯地图小程序api,微信小程序经纬度逆解析地理信息
因为需要在小程序加个定位并加载对应城市信息 然而小程序自带api目前只能获取经纬度不能逆解析,虽然自己解析方式,但是同时也要调用地图,难道用户每次进小程序还要强行打开地图选择地址才定位吗?多麻烦也不利 ...
- C# 微信小程序获取openid sessionkey
项目介绍 1.微信小程序获取openid和session_key 2.后台使用C#开发 项目流程 准备工作 1 获取appid 1.1 下载微信web开发工具 https://developers.w ...
- JavaScript和微信小程序获取IP地址的方法
最近公司新加了一个需求,根据用户登录的IP地址判断是否重复登录,重复登录就进行逼退,那么怎么获取到浏览器的IP地址呢?最后发现搜狐提供了一个JS接口,可以通过它获取到客户端的IP. 接口地址如下: h ...
- 微信小程序获取输入框(input)内容
微信小程序---获取输入框(input)内容 wxml <input placeholder="请输入手机号码" maxlength="11" type= ...
- .Net之微信小程序获取用户UnionID
前言: 在实际项目开发中我们经常会遇到账号统一的问题,如何在不同端或者是不同的登录方式下保证同一个会员或者用户账号唯一(便于用户信息的管理).这段时间就有一个这样的需求,之前有个客户做了一个微信小程序 ...
- 微信小程序获取手机号码看这篇文章就够了
前言 微信小程序获取手机号码,从官方文档到其他博主的文档 零零散散的 (我就是这样看过来 没有一篇满意的 也许是我搜索姿势不对) 依旧是前人栽树 后人乘凉 系列.保证看完 就可以实现获取手机号码功能 ...
- 图解微信小程序---获取电影信息
图解微信小程序---获取电影信息 代码笔记 第一步:编写js文件,调用api获取相对应电影详情信息(注意带入的参数是id不在是榜单的type,电影api的movie后面又斜杠,别忘了,对应的绑定数据的 ...
随机推荐
- [转帖]Kafka可靠性之HW与Leader Epoch
<深入理解Kafka:核心设计与实现原理>是基于2.0.0版本的书 在这本书中,终于看懂了笔者之前提过的几个问题 准备知识 1.leader里存着4个数据:leader_LEO.leade ...
- [转帖]3.3.8. KWR运行期对比报告 KWR DIFF
https://help.kingbase.com.cn/v8/perfor/performance-optimization/performance-optimization-6.html#sys- ...
- [转帖]【存储测试】cosbench存储性能测试工具
一.前言 参考资料: https://blog.csdn.net/QTM_Gitee/article/details/100067724 https://github.com/intel-cloud/ ...
- [转帖]大模型训练,英伟达Turing、Ampere和Hopper算力分析
https://www.eet-china.com/mp/a219195.html 大 GPU 优势在于通过并行计算实现大量重复性计算.GPGPU即通用GPU,能够帮助 CPU 进行非图形相关程序的运 ...
- [转帖]VMware vCenter证书过期解决方法
https://www.yii666.com/blog/395521.html vCenter证书过期解决方法 目录 1 概述 2 详细操作步骤 2.1 检查关键的STS证书是否过期并修复 2.2 检 ...
- 【网络流,dp】Gym102220A Apple Business
Problem Link 有一棵 \(n\) 个点的完全二叉树(点 \(i\) 的父亲是 \(\lfloor i/2\rfloor\)),第 \(i\) 个点有 \(a_i\) 个苹果.现在有 \(m ...
- 一个思路:实现 golang 中的 `__file__` `__line__` 宏
作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 测试 zaplog 发现,开启 caller 的调用,会使 ...
- 常用排序方法——python写法【冒泡、快速排序、TOP-K问题】
1.冒泡排序 相信冒泡排序是很多小伙伴第一个知道的排序算法.它就是每趟排序冒出一个最大(最小)值,相邻两个元素比较,前一个比后一个大,则交换. def bubbleSort(arr): n = len ...
- 【编写环境一】遇到常见python函数处理方式
1.python实现两个一维列表合并成一个二维列表 >>> list1 = [1,2,3,4,4] >>> list2 = [2,3,4,5,2] >> ...
- 守护进程(Python)
#__author__:Kelvin #date:2020/5/10 11:37 import time from multiprocessing import Process def son1(): ...