一周中的第几天
给一个日期,设计一个算法来判断它是对应一周中的哪一天。
输入为三个整数:day、month 和 year,分别表示日、月、年。
您返回的结果必须是这几个值中的一个 {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}。
- 输入:day = 31, month = 8, year = 2019
- 输出:”Saturday”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| let dayOfTheWeek = function(day, month, year) { const week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; const monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]; let days = 365 * (year - 1971) + Math.floor((year - 1969) / 4); for (let i = 0; i < month - 1; ++i) { days += monthDays[i]; } if ((year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) && month >= 3) { days += 1; } days += day; return week[(days + 3) % 7];
};
|