Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript subtracting days from date

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
like image 935
user823527 Avatar asked Jan 27 '12 16:01

user823527


People also ask

How do you subtract days in JavaScript?

You can use d. setDate(d. getDate() + days) with both positive and negative values for days to add and subtract days respectively.

How do you subtract days from a date?

Therefore, you can add or subtract days as easy as adding or minus the number of days in Excel. 1. Select a blank cell you will place the calculating result, type the formula =A2+10, and press the Enter key. Note: For subtracting 10 days from the date, please use this formula =A2–10.

Can I subtract dates in JavaScript?

For finding the difference between dates, the getDate() methods are adapted. Moreover, Math. abs() is also used to subtract two dates in JavaScript.

How do I subtract days from a date in typescript?

let yesterday=new Date(new Date(). getTime() - (1 * 24 * 60 * 60 * 1000)); let last3days=new Date(new Date(). getTime() - (3 * 24 * 60 * 60 * 1000)); We need to minus (no_of_days) * 24 * 60 * 60 * 1000 from current date.


1 Answers

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

like image 130
pong Avatar answered Oct 12 '22 04:10

pong