Created
October 5, 2022 08:18
-
-
Save korvus/266e3dbb4033ddde4efbcce12d4f47bc to your computer and use it in GitHub Desktop.
This file contains 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 getLastDayOfMonth = (year, month, locale = 'en') => { | |
const endDate = new Date(+year, +month+1, 0); | |
return endDate.toLocaleDateString(locale); | |
}; | |
const getFirstDayOfMonth = (year, month, locale = 'en') => { | |
const endDate = new Date(+year, +month, 1); | |
return endDate.toLocaleDateString(locale); | |
}; | |
const formatYYYYMMDDwithHyphens = (date) => { | |
const objDate = new Date(date); | |
const month = objDate.getMonth().toString().padStart(2, '0'); | |
const day = objDate.getDate().toString().padStart(2, '0'); | |
const year = objDate.getFullYear(); | |
return `${year}-${month}-${day}`; | |
}; | |
const totest = new Date('1/2/2021');// mois / jour / année | |
const monthAsString = totest.getMonth().toString(); | |
const yearAsString = totest.getFullYear().toString(); | |
const dayAsString = totest.getDate().toString(); | |
console.log(dayAsString, monthAsString, yearAsString ); | |
const lastDayOfMonth = getLastDayOfMonth(yearAsString, monthAsString); | |
const firstDayOfMonth = getFirstDayOfMonth(yearAsString, monthAsString); | |
console.log(firstDayOfMonth, lastDayOfMonth); | |
const formatedFirst = formatYYYYMMDDwithHyphens(firstDayOfMonth); | |
const formatedLast = formatYYYYMMDDwithHyphens(lastDayOfMonth); | |
console.log(formatedFirst, formatedLast); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment