原文链接
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
const GetPeriod = require("../../utils/getperiod.js");
data: {
period: ''
}
this.data.period = new GetPeriod();
调用方法
在需要的地方调用其方法,例如:
if (this.data.period_index == 1) {
startDate = this.data.period.getWeekStartDate();
endDate = this.data.period.getWeekEndDate();
}
|