add date interval module
This commit is contained in:
@@ -32,6 +32,7 @@
|
||||
"dependencies": {
|
||||
"elasticsearch": "~11.0.1",
|
||||
"lodash": "~4.11.1",
|
||||
"moment": "~2.10.6",
|
||||
"puid": "~1.0.5"
|
||||
}
|
||||
}
|
||||
|
||||
37
src/helpers/index_interval.js
Normal file
37
src/helpers/index_interval.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import moment from 'moment';
|
||||
|
||||
export const intervals = [
|
||||
'y',
|
||||
'M',
|
||||
'w',
|
||||
'd',
|
||||
'h',
|
||||
'm'
|
||||
];
|
||||
|
||||
export const intervalNames = [
|
||||
'year',
|
||||
'month',
|
||||
'week',
|
||||
'day',
|
||||
'hour',
|
||||
'minute'
|
||||
];
|
||||
|
||||
export default function getTimestamp(intervalStr) {
|
||||
const index = intervals.indexOf(intervalStr);
|
||||
if (index === -1) throw new Error('Invalid index interval: ', intervalStr);
|
||||
|
||||
const startType = intervalNames[intervalStr];
|
||||
const m = moment();
|
||||
m.startOf(startType);
|
||||
|
||||
let dateString = 'YYYY-MM-DD';
|
||||
if (startType === 'hour') {
|
||||
dateString += '-HH';
|
||||
}
|
||||
if (startType === 'minute') {
|
||||
dateString += '-HH-mm';
|
||||
}
|
||||
return m.format(dateString);
|
||||
}
|
||||
Reference in New Issue
Block a user