Last active
March 2, 2017 00:13
-
-
Save Guevara-chan/1366dafc2de221ca6ee5e0ced6410914 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
day_of_week = (day, month, year) -> | |
month_len = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # 0 ? Because jan. | |
month_len[2] += 1 unless year % 4 or year % 1000 == 0 # 29th february, you know. | |
unless 0 < month <= 12 # Month boundary check. | |
throw new RangeError("Month index outside of [1..12] range !") | |
unless 0 < day <= month_len[month] # Day index boundary check. | |
throw new RangeError("Day index outside of [1..#{month_len[month]}] range !") | |
#.---Finally some actual calculations: | |
dayz = day + # Amount of days passed. | |
month_len[...month].reduce((a, b) -> a + b) + # Sum(days_in_jan..prev_month) + 1 | |
--year * 365 + year // 4 - year // 1000 # Leap years correction. | |
return dayz %% 7 | |
date2day = (args...) -> | |
['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'][day_of_week args...] | |
## Example... | |
console.log date2day(3, 12, 2014) # => 'wed' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment