javascript Javascript从日期中减去天数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9036838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript subtracting days from date
提问by user823527
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?
从复活节日期我减去 46 天。这通常可以追溯到二月,并且必须考虑闰年。计算的圣灰星期三日期相差一两天,例如,2012 年的圣灰星期三应该是 02/22,2013 年应该是 02/13,2014 年应该是 03/05,2015 年应该是 02/18,2016 年应该是 02/10。月份每次都错了,应该是二月或三月。什么可以解释返回天数和月份的差异?
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
回答by pong
Saw a note in the references not to pass negative values to setDate().
在参考文献中看到一个注释,不要将负值传递给 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 Javascriptdoesn't seem to work any longer
此解决方案通过从 Javascript中的特定日期减去 X 天数来查找日期似乎不再起作用