微信小程序获取本日、本周、本月、本年时间段
原文链接
说明
最近需要用到统计不同时间段内的记录数,所以找了一下现成的工具类。下面就演示一下如何引用到实际项目中。
详细用法请参考: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后面又斜杠,别忘了,对应的绑定数据的 ...
随机推荐
- 使用JavaStream将List转为Map
有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准https://blog.zysicyj.top 首发博客地址 系列文章地址 使用Java Stream将List转换为Map ...
- [转帖]create table INITRANS参数分析
https://www.modb.pro/db/44701 1. 内容介绍 Oracle数据库create table时使用INITRANS参数设置数据块ITL事务槽的数量,确保该数据块上 并发事务数 ...
- [转帖]wmic命令介绍
https://www.jianshu.com/p/3e1a5a8fa23b How to Get Your System Serial Number PS C:\windows\system32&g ...
- Docker 镜像减少体积的思路和方法
Docker 镜像减少体积的思路和方法 背景 有一个项目感觉镜像有点大 这边同事喊着一起帮忙处理一下. 今天基本上就在客户现场进行处理了. 想着应该把自己想到的东西整理一下. 整体思路 1. 清理do ...
- unzip 解压缩存在Bug-- 这个方法不行啊
linux中解压大于4G的zip压缩包(已解决) tar -zxvf 压缩包名.zip
- 2024了,我不想再用AOP收集业务操作日志了 | 京东云技术团队
0.背景 在近期的项目中,系统涉及到针对系统的业务操作日志统计功能,由于本系统位于业务链路的中心环节,负责接收上游系统的数据,并将基于用户操作产生的数据传递至下游系统,鉴于业务链路的复杂性和操作场景的 ...
- net core部署iis执行此操作时出错web.config
页面访问会报服务器内部错误,你点对应的IIS下的默认页面或模块会出现下面的错语. 请到官网下载对应的运行时:https://www.microsoft.com/net/download 如果是服务器, ...
- 4G5G和上网带宽与下载速度的换算方法
前言 2020年5G越来越火热,而且运营商多次推出免费宽带升级,免费升级到100M,20M升级50M等等.很多人疑惑我们平时 的下载速度也就几百K或者有时候能上1M,但是就算升级到10M的宽带,也从来 ...
- GPT大语言模型引爆强化学习与语言生成模型的热潮、带你了解RLHF。
GPT大语言模型引爆强化学习与语言生成模型的热潮.带你了解RLHF. 随着 ChatGPT 的爆火,强化学习(Reinforcement Learning)和语言生成模型(Language Model ...
- 守护进程(Python)
#__author__:Kelvin #date:2020/5/10 11:37 import time from multiprocessing import Process def son1(): ...