Last active
October 19, 2018 15:55
-
-
Save tng-sy/8675f57e04e487cd1f40821e39fa0be3 to your computer and use it in GitHub Desktop.
Date JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const daysNames = [ | |
'Dimanche', | |
'Lundi', | |
'Mardi', | |
'Mercredi', | |
'Jeudi', | |
'Vendredi', | |
'Samedi' | |
]; | |
const monthsNames = [ | |
'Janvier', | |
'Février', | |
'Mars', | |
'Avril', | |
'Mai', | |
'Juin', | |
'Juillet', | |
'Août', | |
'Septembre', | |
'Octobre', | |
'Novembre', | |
'Décembre' | |
]; | |
const itemModel = ({ key, label }) => ({ | |
key, | |
label | |
}); | |
const dayModel = itemModel; | |
const yearModel = itemModel; | |
const getCurrentDate = () => { | |
const currentDate = new Date(); | |
return { | |
year: currentDate.getFullYear(), | |
month: currentDate.getMonth(), | |
day: currentDate.getDate() | |
}; | |
}; | |
const filterDaysToCurrent = (days, month) => { | |
const currentDate = getCurrentDate(); | |
if (month < currentDate.month) { | |
return days; | |
} else if (month != currentDate.month) { | |
return []; | |
} | |
return days.filter((day) => (day.key <= currentDate.day)); | |
} | |
const getYears = (max = 5) => { | |
let currentDate = getCurrentDate(); | |
let arr = new Array(max).fill(undefined); | |
return arr.map((value, index) => { | |
const year = (currentDate.year - index); | |
return { | |
id: year, | |
value: year | |
} | |
}) | |
}; | |
const getMonths = (year = null) => { | |
let currentDate = getCurrentDate(); | |
let selectedYear = year; | |
if (year === null) { | |
selectedYear = currentDate.year; | |
} | |
if (selectedYear < currentDate.year) { | |
return monthsNames; | |
} else if (selectedYear !== currentDate.year) { | |
return []; | |
} else { | |
return monthsNames.slice(0, currentDate.month + 1); | |
} | |
}; | |
const getDays = (month, year) => { | |
if (month > 12) { | |
throw 'fn getDaysArr: wrong parameter month'; | |
} | |
let curMonth = month - 1; | |
let selectedDate = new Date(year, curMonth, 1); | |
const res = []; | |
while (selectedDate.getMonth() == curMonth) { | |
res.push(dayModel({ | |
key: selectedDate.getDate(), | |
label: daysNames[selectedDate.getDay()] | |
})); | |
selectedDate.setDate(selectedDate.getDate() + 1); | |
} | |
return filterDaysToCurrent(res, curMonth); | |
} | |
const getWeeks = (month, year) => { | |
const days = getDays(month, year); | |
const weeks = []; | |
let currentWeek = 0; | |
console.log('days', days); | |
days.map((day) => { | |
if (day.label === 'Lundi') { | |
weeks[currentWeek] = { | |
from: day.key, | |
to: 0 | |
}; | |
} | |
}); | |
console.log('weeks', weeks); | |
return 'ok'; | |
const lastIndex = weeks.length - 1; | |
weeks[lastIndex].to = weeks[lastIndex].from + 6; | |
return weeks; | |
} | |
// Récupérer les années à partir de celle courante : | |
const yearsList = getYears(5); | |
//console.log('yearsList', yearsList); | |
/** | |
* Résultat : | |
[ | |
{ id: 2018, value: 2018 }, | |
{ id: 2017, value: 2017 }, | |
{ id: 2016, value: 2016 }, | |
{ id: 2015, value: 2015 }, | |
{ id: 2014, value: 2014 }, | |
] | |
*/ | |
// Récupérer les mois d'une année : | |
// La fonction va retourner la liste de tous les mois si ce n'est pas l'année courante. | |
// Sinon elle retourne la liste de mois jusqu'à celui courant. | |
const monthsList = getMonths(2018); | |
//console.log('monthsList', monthsList); | |
/** | |
* Résultat : | |
[ | |
'Janvier', | |
'Février', | |
'Mars', | |
'Avril', | |
'Mai', | |
'Juin', | |
'Juillet', | |
'Août', | |
'Septembre', | |
'Octobre', | |
] | |
*/ | |
// Récupérer la liste de jours d'un mois et d'une année : | |
// La fonction va retourner une liste de tous les jours du mois si ce n'est pas le mois et l'année courante. | |
// Sinon elle retourne une liste jusqu'au jour actuel du mois courant. | |
const daysList = getDays(10, 2018); | |
//console.log('daysList', daysList); | |
/** | |
* Résultat : | |
[ | |
{ key: 1, label: 'Lundi' }, | |
{ key: 2, label: 'Mardi' }, | |
... | |
{ key: 19, label: 'Vendredi' }, | |
] | |
*/ | |
// Récupérer la liste des semaines d'un mois et d'une année : | |
const weeksList = getWeeks(9, 2018); | |
console.log('weeksList', weeksList); | |
/** | |
* Résultat : | |
[ | |
{ from: 1, to: 7 } | |
{ from: 8, to: 14 } | |
{ from: &, to: 21 } | |
] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment