add date interval module

This commit is contained in:
2016-04-22 11:25:06 -07:00
parent df8bd2123b
commit d357971554
2 changed files with 38 additions and 0 deletions

View 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);
}