Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
179
rated 0 times [  180] [ 1]  / answers: 1 / hits: 20195  / 13 Years ago, fri, january 27, 2012, 12:00:00

This code where I subtract days from a date does not get the right results.



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;
}


From the Easter date I subtract 46 days. This usually goes back to February and must account for leap years. The Ash Wednesday dates calculated are off by one or two days, e.g. for 2012 Ash Wednesday should be 02/22, for 2013 should be 02/13, for 2014 should be 03/05, for 2015 should be 02/18, for 2016 should be 02/10. The month is wrong every time, should be February or March. What could explain the difference in the days returned and the month?



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

More From » date

 Answers
24

Saw a note in the references not to pass negative values to setDate().



Try this instead:



var Ash_Wednesday = new Date (  
Easter_date.getFullYear(),
Easter_date.getMonth(),
(Easter_date.getDate()-46)
)


This solution Finding date by subtracting X number of days from a particular date in Javascript doesn't seem to work any longer


[#87774] Wednesday, January 25, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
parker

Total Points: 259
Total Questions: 109
Total Answers: 97

Location: Zambia
Member since Thu, Jun 25, 2020
4 Years ago
;