Last active
November 5, 2019 14:16
-
-
Save nicolai86/5887239 to your computer and use it in GitHub Desktop.
do it yourself calendar.
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
isSunday = (date) -> | |
return date.getDay() is 0 | |
nextDay = (date, days = 1) -> | |
newDate = new Date(date.getTime()) | |
newDate.setDate(date.getDate() + days) | |
return newDate | |
prevDay = (date, days = 1) -> | |
newDate = new Date(date.getTime()) | |
newDate.setDate(date.getDate() - days) | |
return newDate | |
nextMonth = (date) -> | |
if date.getMonth() is 11 | |
return new Date(date.getFullYear() + 1, 0, 1) | |
else | |
return new Date(date.getFullYear(), date.getMonth() + 1, 1) | |
dayOfYear = (date) -> | |
janFirst = new Date(date.getFullYear(),0,1) | |
return Math.ceil((date - janFirst) / 86400000) | |
firstAvailableDate = new Date(2012,11,1) | |
year = firstAvailableDate.getFullYear() | |
month = firstAvailableDate.getMonth() | |
firstDayOfMonth = new Date(year, month, 1) | |
daysToDisplayPrior = if isSunday(firstDayOfMonth) then -6 else (if firstDayOfMonth.getDay() > 1 then -firstDayOfMonth.getDay() + 1 else 0) | |
firstMondayInCalendar = null | |
if month is 0 | |
firstMondayInCalendar = new Date(year - 1, 12, daysToDisplayPrior) | |
else | |
firstMondayInCalendar = new Date(year, month, daysToDisplayPrior) | |
lastDayInMonth = prevDay(nextMonth(firstDayOfMonth)) | |
daysInMonth = dayOfYear(lastDayInMonth) - dayOfYear(firstDayOfMonth) | |
daysToDisplay = daysInMonth + Math.abs(daysToDisplayPrior) | |
weeksToDisplay = Math.ceil(parseFloat(daysToDisplay / 7, 10)) | |
results = [] | |
for week in [0...weeksToDisplay] | |
datesInWeek = [] | |
for wday in [1..7] | |
datesInWeek.push nextDay(firstMondayInCalendar, week * 7 + wday) | |
results.push datesInWeek |
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
require 'date' | |
year = 2012 | |
month = 12 | |
first_day_of_month = Date.new(year, month, 1) | |
days_to_display_prior = (first_day_of_month.sunday? ? -6 : (first_day_of_month.wday > 1 ? -first_day_of_month.wday : 1)) | |
first_monday_in_calendar = if month - 1 == 0 | |
Date.new(year - 1, 12, days_to_display_prior) | |
else | |
Date.new(year, month - 1, days_to_display_prior) | |
end | |
7.times.map { |wday| first_monday_in_calendar.next_day(wday) } | |
last_day_in_month = first_day_of_month.next_month.prev_day | |
days_in_month = last_day_in_month.yday - first_day_of_month.yday | |
days_to_display = days_in_month + days_to_display_prior.abs | |
weeks_to_display = weeks_to_display = (days_to_display.to_f / 7).ceil | |
weeks_to_display.times.map { |week| | |
7.times.map { |wday| | |
first_monday_in_calendar.next_day(week * 7 + wday).strftime("%Y-%m-%d") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment