Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 2x 2x 2x 5x 9x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 2x 1x 3x 3x 5x 2x | /**
* 获取过去一段时间的起始日期,如3月前第1天,2周前第1天,3小时前整点
* @method getLastStart
* @param {Number|Date} time 实际时间
* @param {String} type 单位时间类型,可选 ['year', 'month', 'week', 'day', 'hour']
* @param {Number} count 多少单位时间之前
* @return {Date} 最近单位时间的起始时间对象
* @example
* import { getLastStartTime } from '@spore-ui/tskit';
* const time = getLastStartTime(
* new Date('2018-10-25'),
* 'month',
* 0
* ).getTime(); // 1538323200000
* new Date(time); // Mon Oct 01 2018 00:00:00 GMT+0800 (中国标准时间)
*/
import { getUTCDate } from './getUTCDate';
import {
getTimeSplit,
TypeTimeUnit,
} from './getTimeSplit';
import { TypeDate } from '../types';
const enum UnitTime {
HOUR = 60 * 60 * 1000,
DAY = 24 * 60 * 60 * 1000,
}
export function getLastStartTime(
time: TypeDate,
Itype: TypeTimeUnit = 'day',
count: number = 0,
): Date {
const localTime = new Date(time).getTime();
const utcTime = getUTCDate(time);
let stamp = utcTime;
let year;
let month;
let allMonths;
let unit;
if (!type) {
throw new Error('required param type');
}
if (type === 'year') {
year = utcTime.getUTCFullYear();
year -= count;
stamp = new Date(`${year}/1/1`);
} else if (type === 'month') {
year = utcTime.getUTCFullYear();
month = utcTime.getUTCMonth();
allMonths = year * 12 + month - count;
year = Math.floor(allMonths / 12);
month = allMonths - year * 12;
month += 1;
stamp = new Date(`${year}/${month}/1`);
} else {
unit = UnitTime.HOUR;
if (type === 'day') {
unit = UnitTime.DAY;
}
if (type === 'week') {
unit = 7 * UnitTime.DAY;
}
const newLocalTime = localTime - count * unit;
stamp = getTimeSplit(newLocalTime, type);
}
return stamp;
}
export default getLastStartTime;
|