Javascript从日期减去天数 [英] Javascript subtracting days from date

查看:114
本文介绍了Javascript从日期减去天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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(年);
Ash_Wednesday.setDate(Easter_date.getDate() - 46);
返回Ash_Wednesday;
}

从复活节的日期我减去46天。这通常可以追溯到二月份,必须考虑到闰年。灰星期三计算的时间是关闭一到两天,例如对于2012年Ash星期三应该是02/22,2013年应该是02/13,2014年应该是03/05,2015年应该是02/18,2016年应该是02/10。每个月都是错误的,应该是二月或三月。什么可以解释返回日期和月份的差异?

 年份:2012灰色星期三月11日星期三第23天复活节:4复活节:8 
年份:2013灰色星期三月12日灰色周三天16复活节月份:3复活节:31
年份:2014灰色星期三月12日灰色周三第5天复活节月份:4复活节:20
年份:2015灰色星期三月份11灰色周三第20天复活节月份:4复活节:5
年份:2016灰色星期三月12日灰色星期三第12天复活节月份:3复活节:27
年份:2017灰色星期三月12日灰色星期三第1天复活节月份:4复活节:16
年份:2018灰色星期三月份11灰色星期三天16复活节月份:4复活节:1
年份: 2019 Ash星期三月12日Ash星期三第6天复活节月份:4复活节:21
年份:2020灰色星期三月份11灰色星期三天27复活节月份:4复活节:12
年份:2021灰色星期三11 Ash星期三19日复活节:4复活节:4
年份:2022灰色星期三月12日灰色星期三天2复活节月份:4复活节:17
年份:2023灰色星期三月份11灰色星期三天24复活节月份:4复活节:9
年份:2024 Ash星期三月12日星期三16日复活节复活节3天复活节31
年份:2025灰色星期三月12日灰色星期三第5天复活节月份:4复活节:20
年份:2026灰色星期三11个灰星期三20日复活节:4复活节:5
年份:2027灰色星期三月12日灰色星期三第13天复活节月份:3复活节:28
年份:2028灰色星期三月12日灰色星期三第1天复活节月份:4复活节:16
年份:2029灰色星期三月份11灰色星期三天16复活节月份:4复活节:1
年份:2030灰色星期三月12日灰色星期三第6天复活节月份:4复活节日期:21
年份:2031灰色星期三月份11灰色星期三天28复活节月份:4复活节:13
年份:2032灰色星期三月12日灰色星期三第13天复活节月份:3复活节:28
Ye ar:2033 Ash星期三月12日Ash星期三第2天复活节月份:4复活节:17
年份:2034 Ash星期三月11日Ash星期三24天复活节月份:4复活节:9
年份:2035 Ash星期三月12日星期三天10复活节月份:3复活节:25
年份:2036灰色星期三月11日灰色星期三天28复活节月份:4复活节:13
年份:2037灰色星期三月11日灰星期三20日复活节:4复活节:5
年份:2038 Ash星期三月12日Ash星期三10天复活节月份:4复活节:25
年份:2039灰色星期三月11日灰色星期三第25天复活节月份:4复活节:10
年份:2040灰色星期三月份11灰色周三天16复活节月份:4复活节:1
年份:2041灰色星期三月12日灰色星期三第6天复活节月份:4复活节日:21
年份:2042灰色星期三月份11灰色星期三天21复活节月份:4复活节:6


解决方案

在引用中看到一个注释,而不是将负值传递给setDate()。



改为:

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

此解决方案通过从Javascript中的特定日期减去X天数查找日期似乎不再起作用p>

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

解决方案

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

这篇关于Javascript从日期减去天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆