mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2024-11-01 14:10:29 +08:00
几个常用的函数
This commit is contained in:
parent
9a66469241
commit
b40e2d8991
@ -2,6 +2,7 @@ import { Inject, Provide } from '@midwayjs/decorator';
|
||||
import { Context } from '@midwayjs/koa';
|
||||
import * as ipdb from 'ipip-ipdb';
|
||||
import * as _ from 'lodash';
|
||||
import * as moment from 'moment';
|
||||
|
||||
/**
|
||||
* 帮助类
|
||||
@ -64,4 +65,97 @@ export class Utils {
|
||||
sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得最近几天的日期集合
|
||||
* @param recently
|
||||
*/
|
||||
getRecentlyDates(recently, format = 'YYYY-MM-DD') {
|
||||
moment.locale('zh-cn');
|
||||
const dates = [];
|
||||
for (let i = 0; i < recently; i++) {
|
||||
dates.push(moment().subtract(i, 'days').format(format));
|
||||
}
|
||||
return dates.reverse();
|
||||
}
|
||||
/**
|
||||
* 获得最近几个月的月数
|
||||
* @param recently
|
||||
*/
|
||||
getRecentlyMonths(recently, format = 'YYYY-MM') {
|
||||
moment.locale('zh-cn');
|
||||
const dates = [];
|
||||
const date = moment(Date.now()).format('YYYY-MM');
|
||||
for (let i = 0; i < recently; i++) {
|
||||
dates.push(moment(date).subtract(i, 'months').format(format));
|
||||
}
|
||||
return dates.reverse();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据开始和结束时间,获得时间段内的日期集合
|
||||
* @param start
|
||||
* @param end
|
||||
*/
|
||||
getBetweenDays(start, end, format = 'YYYY-MM-DD') {
|
||||
moment.locale('zh-cn');
|
||||
const dates = [];
|
||||
const startTime = moment(start).format(format);
|
||||
const endTime = moment(end).format(format);
|
||||
const days = moment(endTime).diff(moment(startTime), 'days');
|
||||
for (let i = 0; i <= days; i++) {
|
||||
dates.push(moment(startTime).add(i, 'days').format(format));
|
||||
}
|
||||
return dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据开始和结束时间,获得时间段内的月份集合
|
||||
* @param start
|
||||
* @param end
|
||||
*/
|
||||
getBetweenMonths(start, end, format = 'YYYY-MM') {
|
||||
moment.locale('zh-cn');
|
||||
const dates = [];
|
||||
const startTime = moment(start).format(format);
|
||||
const endTime = moment(end).format(format);
|
||||
const months = moment(endTime).diff(moment(startTime), 'months');
|
||||
for (let i = 0; i <= months; i++) {
|
||||
dates.push(moment(startTime).add(i, 'months').format(format));
|
||||
}
|
||||
return dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据开始和结束时间,获得时间段内的小时集合
|
||||
* @param start
|
||||
* @param end
|
||||
*/
|
||||
getBetweenHours(start, end, format = 'YYYY-MM-DD HH') {
|
||||
moment.locale('zh-cn');
|
||||
const dates = [];
|
||||
const startTime = moment(start).format(format);
|
||||
const endTime = moment(end).format(format);
|
||||
const hours = moment(endTime).diff(moment(startTime), 'hours');
|
||||
for (let i = 0; i <= hours; i++) {
|
||||
dates.push(moment(startTime).add(i, 'hours').format(format));
|
||||
}
|
||||
return dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段转驼峰法
|
||||
* @param obj
|
||||
* @returns
|
||||
*/
|
||||
toCamelCase(obj) {
|
||||
let camelCaseObject = {};
|
||||
for (let i in obj) {
|
||||
let camelCase = i.replace(/([-_][a-z])/gi, $1 => {
|
||||
return $1.toUpperCase().replace('-', '').replace('_', '');
|
||||
});
|
||||
camelCaseObject[camelCase] = obj[i];
|
||||
}
|
||||
return camelCaseObject;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user