我从日期中减去天数的代码无法获得正确的结果。
function get_Ash_Wednesday_date (year, Easter_month, Easter_day) {
var Easter_date = new Date();
Easter_date.setFullYear(year, Easter_month-1, Easter_day);
var Ash_Wednesday = new Date();
Ash_Wednesday.setFullYear(year);
Ash_Wednesday.setDate(Easter_date.getDate()-46);
return Ash_Wednesday;
}
从复活节开始,我减去46天。这通常可以追溯到二月,必须考虑到闰年。计算的灰星期三日期是一天或两天,例如对于2012年灰烬星期三应该是02/22,2013年应该是02 / 13,2014应该是03 / 05,2015应该是02 / 18,2016应该是02/10。这个月每次都错了,应该是2月或3月。什么可以解释返回的日子和月份的差异?
Year: 2012 Ash Wednesday month 11 Ash Wednesday day 23 Easter Month: 4 Easter Day: 8
Year: 2013 Ash Wednesday month 12 Ash Wednesday day 16 Easter Month: 3 Easter Day: 31
Year: 2014 Ash Wednesday month 12 Ash Wednesday day 5 Easter Month: 4 Easter Day: 20
Year: 2015 Ash Wednesday month 11 Ash Wednesday day 20 Easter Month: 4 Easter Day: 5
Year: 2016 Ash Wednesday month 12 Ash Wednesday day 12 Easter Month: 3 Easter Day: 27
Year: 2017 Ash Wednesday month 12 Ash Wednesday day 1 Easter Month: 4 Easter Day: 16
Year: 2018 Ash Wednesday month 11 Ash Wednesday day 16 Easter Month: 4 Easter Day: 1
Year: 2019 Ash Wednesday month 12 Ash Wednesday day 6 Easter Month: 4 Easter Day: 21
Year: 2020 Ash Wednesday month 11 Ash Wednesday day 27 Easter Month: 4 Easter Day: 12
Year: 2021 Ash Wednesday month 11 Ash Wednesday day 19 Easter Month: 4 Easter Day: 4
Year: 2022 Ash Wednesday month 12 Ash Wednesday day 2 Easter Month: 4 Easter Day: 17
Year: 2023 Ash Wednesday month 11 Ash Wednesday day 24 Easter Month: 4 Easter Day: 9
Year: 2024 Ash Wednesday month 12 Ash Wednesday day 16 Easter Month: 3 Easter Day: 31
Year: 2025 Ash Wednesday month 12 Ash Wednesday day 5 Easter Month: 4 Easter Day: 20
Year: 2026 Ash Wednesday month 11 Ash Wednesday day 20 Easter Month: 4 Easter Day: 5
Year: 2027 Ash Wednesday month 12 Ash Wednesday day 13 Easter Month: 3 Easter Day: 28
Year: 2028 Ash Wednesday month 12 Ash Wednesday day 1 Easter Month: 4 Easter Day: 16
Year: 2029 Ash Wednesday month 11 Ash Wednesday day 16 Easter Month: 4 Easter Day: 1
Year: 2030 Ash Wednesday month 12 Ash Wednesday day 6 Easter Month: 4 Easter Day: 21
Year: 2031 Ash Wednesday month 11 Ash Wednesday day 28 Easter Month: 4 Easter Day: 13
Year: 2032 Ash Wednesday month 12 Ash Wednesday day 13 Easter Month: 3 Easter Day: 28
Year: 2033 Ash Wednesday month 12 Ash Wednesday day 2 Easter Month: 4 Easter Day: 17
Year: 2034 Ash Wednesday month 11 Ash Wednesday day 24 Easter Month: 4 Easter Day: 9
Year: 2035 Ash Wednesday month 12 Ash Wednesday day 10 Easter Month: 3 Easter Day: 25
Year: 2036 Ash Wednesday month 11 Ash Wednesday day 28 Easter Month: 4 Easter Day: 13
Year: 2037 Ash Wednesday month 11 Ash Wednesday day 20 Easter Month: 4 Easter Day: 5
Year: 2038 Ash Wednesday month 12 Ash Wednesday day 10 Easter Month: 4 Easter Day: 25
Year: 2039 Ash Wednesday month 11 Ash Wednesday day 25 Easter Month: 4 Easter Day: 10
Year: 2040 Ash Wednesday month 11 Ash Wednesday day 16 Easter Month: 4 Easter Day: 1
Year: 2041 Ash Wednesday month 12 Ash Wednesday day 6 Easter Month: 4 Easter Day: 21
Year: 2042 Ash Wednesday month 11 Ash Wednesday day 21 Easter Month: 4 Easter Day: 6
答案 0 :(得分:13)
在引用中看到一条注释,不要将负值传递给setDate()。
请改为尝试:
var Ash_Wednesday = new Date (
Easter_date.getFullYear(),
Easter_date.getMonth(),
(Easter_date.getDate()-46)
)
此解决方案Finding date by subtracting X number of days from a particular date in Javascript似乎不再有效