Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Uso de date-fns

![](https://raw.githubusercontent.com/Angelozam17/challenge-javascript-12/master/src/assets/test.png)


La manipulación de fechas en JS puede ser compleja usando la lib de date-dns hace que sea más facíl.

# RETO 1
Expand Down
Binary file added src/assets/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 58 additions & 1 deletion src/filter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,63 @@
const { format, sub, startOfYear, endOfYear, startOfMonth } = require('date-fns');
const { es } = require('date-fns/locale')

const makeFilter = (date) => {
let filters = [];
let filters = [
{
label: 'Últimos 7 días',
startAt: format(sub(date, { days: 7 }), 'yyyy/MM/dd'),
endAt: format(date, 'yyyy/MM/dd')
},
{
label: 'Últimos 28 días',
startAt: format(sub(date, { days: 28 }), 'yyyy/MM/dd'),
endAt: format(date, 'yyyy/MM/dd')
},
{
label: 'Últimos 90 días',
startAt: format(sub(date, { days: 90 }), 'yyyy/MM/dd'),
endAt: format(date, 'yyyy/MM/dd')
},
{
label: 'Últimos 365 días',
startAt: format(sub(date, { years: 1 }), 'yyyy/MM/dd'),
endAt: format(date, 'yyyy/MM/dd')
},
{
label: format( startOfYear( sub( date, { years: 1 } ) ), 'yyyy'),
startAt: format( startOfYear( sub( date, { years: 1 } ) ), 'yyyy/MM/dd'),
endAt: format( endOfYear( sub( date, { years: 1 } ) ), 'yyyy/MM/dd')
},
{
label: format( startOfYear( sub( date, { years: 2 } ) ), 'yyyy'),
startAt: format( startOfYear( sub( date, { years: 2 } ) ), 'yyyy/MM/dd'),
endAt: format( endOfYear( sub( date, { years: 2 } ) ), 'yyyy/MM/dd')
},
{
label: format( startOfYear( sub( date, { years: 3 } ) ), 'yyyy'),
startAt: format( startOfYear( sub( date, { years: 3 } ) ), 'yyyy/MM/dd'),
endAt: format( endOfYear( sub( date, { years: 3 } ) ), 'yyyy/MM/dd')
},
{
label: format( endOfYear( sub( date, { years: 1 } ) ), 'MMMM', { locale: es }),
startAt: format( startOfMonth( endOfYear( sub( date, { years: 1 } ) ) ), 'yyyy/MM/dd'),
endAt: format( endOfYear( sub( date, { years: 1 } ) ), 'yyyy/MM/dd')
},
{
label: format( sub( endOfYear( sub( date, { years: 1 } ) ), { months: 1 } ), 'MMMM', { locale: es }),
startAt: format( sub( startOfMonth( endOfYear( sub( date, { years: 1 } ) ) ), { months: 1 } ), 'yyyy/MM/dd'),
endAt: format( sub( endOfYear( sub( date, { years: 1 } ) ), { months: 1 } ), 'yyyy/MM/dd')
},
{
label: format( sub( endOfYear( sub( date, { years: 1 } ) ), { months: 2 } ), 'MMMM', { locale: es }),
startAt: format( sub( startOfMonth( endOfYear( sub( date, { years: 1 } ) ) ), { months: 2 } ), 'yyyy/MM/dd'),
endAt: format( sub( endOfYear( sub( date, { years: 1 } ) ), { months: 2 } ), 'yyyy/MM/dd')
}
];
return filters;
}


console.log( makeFilter( new Date(2020,0,1) ) )

module.exports = { makeFilter };