-
-
Save hazho/0d2fc6b768294a126b22c5745c3d4f66 to your computer and use it in GitHub Desktop.
javascript code to generate calendar. By using this you can create your own customized calendar UI.
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 daysInMonth = (iYear, iMonth) => 32 - new Date(iYear, iMonth, 32).getDate() | |
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] | |
const getSelectedMonthDates = (selectedYear, selectedMonth) => { | |
const calendarDates = [] | |
const firstDay = (new Date(selectedYear, selectedMonth)).getDay() // 0 = 'Sunday', 6 = 'Saturday' | |
const days = daysInMonth(selectedYear, selectedMonth) // how many days in a month | |
let x = 0 | |
for (let i = 0; i < days; i += 1) { | |
if (calendarDates.length === 0) { | |
calendarDates.push([]) | |
if (firstDay !== 0) { | |
for (let y = 0; y < firstDay; y += 1) { | |
const getYear = months[selectedMonth] === 'January' ? selectedYear - 1 : selectedYear | |
const getMonth = months[selectedMonth] === 'January' ? 11 : selectedMonth - 1 | |
const previousMonthDays = daysInMonth(getYear, getMonth) | |
calendarDates[x].unshift({ date: previousMonthDays - y, month: getMonth, year: getYear }) | |
} | |
calendarDates[x].push({ date: i + 1, month: selectedMonth, year: selectedYear }) | |
} else { | |
calendarDates[x].push({ date: i + 1, month: selectedMonth, year: selectedYear }) | |
} | |
} else if (calendarDates.length !== 0) { | |
if (calendarDates[x].length < 7) { | |
calendarDates[x].push({ date: i + 1, month: selectedMonth, year: selectedYear }) | |
} else { | |
calendarDates.push([{ date: i + 1, month: selectedMonth, year: selectedYear }]) | |
x += 1 | |
} | |
} | |
} | |
const lastIndex = calendarDates.length - 1 | |
if (calendarDates[lastIndex].length < 7) { | |
const leftOverDay = 7 - calendarDates[lastIndex].length | |
for (let i = 0; i < leftOverDay; i += 1) { | |
const getMonth = months[selectedMonth] === 'December' ? 0 : selectedMonth + 1 | |
const getYear = months[selectedMonth] === 'December' ? selectedYear + 1 : selectedYear | |
calendarDates[lastIndex].push({ date: i + 1, month: getMonth, year: getYear }) | |
} | |
} | |
return calendarDates | |
} | |
const presentDate = new Date() | |
const presentMonth = presentDate.getMonth() // 0 - 11, 0 = 'January' | |
const presentYear = presentDate.getFullYear() | |
const dates = getSelectedMonthDates(presentYear, presentMonth) | |
console.log(dates) | |
/* | |
Expected output 2D array | |
The nested array length will always be 7, index 0 = 'Sunday', index 6 = 'Saturday | |
e.g. | |
[ | |
[{ month: 1, day: 31 }, { month: 2, day: 1 }, { month: 2, day: 2 }, { month: 2, day: 3 }, { month: 2, day: 4 }, { month: 2, day: 5 }, { month: 2, day: 6 }], | |
... | |
] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment