UNPKG

@geniucode/common-utils

Version:

Common utils

58 lines 1.99 kB
export const daysMapping = () => { const days = {}; days[0] = 'Sun'; days[1] = 'Mon'; days[2] = 'Tue'; days[3] = 'Wed'; days[4] = 'Thu'; days[5] = 'Fri'; days[6] = 'Sat'; return days; }; export const daysMappingRev = () => { const days = {}; days['Sun'] = 0; days['Mon'] = 1; days['Tue'] = 2; days['Wed'] = 3; days['Thu'] = 4; days['Fri'] = 5; days['Sat'] = 6; return days; }; export const getShortISODate = (dayDate = new Date()) => { return dayDate.toISOString().split('T')[0]; }; export const getWeekNumber = (currentDate = new Date()) => { const startDate = new Date(currentDate.getFullYear(), 0, 1); const days = Math.floor((currentDate - startDate) / (24 * 60 * 60 * 1000)); const weekNumber = Math.ceil(days / 7); return weekNumber; }; export const getDayInWeek = (dayDate = new Date()) => { const _daysMapping = daysMapping(); const d = dayDate; const day = d.getDay(); return _daysMapping[day]; }; export const getXDaysAgo = (x = 1) => { const today = new Date(); const xAgo = new Date(today); xAgo.setDate(xAgo.getDate() - x); return xAgo; }; export const chartFilterOptions = [ { label: 'Today', key: { label: 'day', value: new Date() } }, { label: 'Yesterday', key: { label: 'day', value: getXDaysAgo(1) } }, { label: '2 Days Ago', key: { label: 'day', value: getXDaysAgo(2) } }, { label: 'This Week', key: { label: 'week', value: getWeekNumber() } }, { label: 'This Month', key: { label: 'month', value: getShortISODate().split('-')[1] } }, { label: 'This Year', key: { label: 'year', value: getShortISODate().split('-')[0] } }, ]; export const getDatesDiffInSeconds = (date1, date2 = new Date()) => { const date2InSeconds = Math.floor(date2.getTime() / 1000); const date1InSeconds = Math.floor(date1.getTime() / 1000); const diff = date2InSeconds - date1InSeconds; return diff; }; //# sourceMappingURL=date.js.map