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 | 1x 1x 1x 1x 6x 6x 6x 6x 1x 5x 5x 1x 1x 1x 4x 1x 1x 1x 1x 1x 1x 1x 3x 3x 2x 3x 1x 3x 3x 5x 1x | /**
* 获取过去一段时间的起始日期,如3月前第1天,2周前第1天,3小时前整点
* @method date/getLastStart
* @param {Number|Date} time 实际时间
* @param {String} type 单位时间类型,可选 ['year', 'month', 'week', 'day', 'hour']
* @param {Number} count 多少单位时间之前
* @returns {Date} 最近单位时间的起始时间对象
* @example
* var $getLastStart = require('@spore-ui/kit/packages/date/getLastStart');
* var time = $getLastStart(
* new Date('2018-10-25'),
* 'month',
* 0
* ).getTime(); // 1538323200000
* new Date(time); // Mon Oct 01 2018 00:00:00 GMT+0800 (中国标准时间)
*/
var $getTimeSplit = require('./getTimeSplit');
var $getUTCDate = require('./getUTCDate');
var HOUR = 60 * 60 * 1000;
var DAY = 24 * 60 * 60 * 1000;
function getLastStart(time, type, count) {
var localTime = new Date(time);
var utcTime = $getUTCDate(time);
var stamp = utcTime;
var year;
var month;
var allMonths;
var unit;
if (!type) {
throw new Error('required param type');
}
count = count || 0;
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].join('/'));
} else {
unit = HOUR;
if (type === 'day') {
unit = DAY;
}
if (type === 'week') {
unit = 7 * DAY;
}
var newLocalTime = localTime - count * unit;
stamp = $getTimeSplit(newLocalTime, type);
}
return stamp;
}
module.exports = getLastStart;
|