[英]How to convert date values to number of days
I am trying to add days to a date.我正在尝试将日期添加到日期。 I am taking input from the user where I have separate input boxes for the each field like no.
我从用户那里获取输入,其中每个字段都有单独的输入框,例如 no。 of years, no of months and no of days like this.
几年,没有几个月,没有这样的日子。
as you can see in the 2nd input field row I am accepting no of years, days, months, etc. and in First row I am getting a proper date like: 2020 10 05 00 00 00 and then passing them to date constructor to gate date.正如您在第二个输入字段行中看到的那样,我不接受年、日、月等,在第一行中,我得到了一个正确的日期,例如:2020 10 05 00 00 00,然后将它们传递给日期构造函数到门日期。
ex.前任。
Date firstdate = new Date(2020, 10, 05, 00, 00,00);
and I am adding days to the above date using the following function我正在使用以下 function 在上述日期上添加天数
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
I am adding days to the previous date by calling a function like this.我通过像这样调用 function 来增加前一个日期的天数。 var newdate = firstdate.addDays(totaldays);
var newdate = firstdate.addDays(totaldays);
Now the problem is while I am calculating days it is not including leap years and months with their specified date.现在的问题是,当我计算天数时,它不包括带有指定日期的闰年和月份。 because I am calculating days like this way:
因为我是这样计算天数的:
totaldays = (years * 365) + (months * 30) + days;
What is the way so I can add days perfectly?有什么方法可以完美地增加天数? for example, if the user entered date like
例如,如果用户输入日期
2020 12 10 00 00 00 2020 12 10 00 00 00
and add the number of days and years like this并像这样添加天数和年数
1 year 3 months 0 days 00 00 00 1 年 3 个月 0 天 00 00 00
so it should ideally calculate 3 months that is January, February, march, as the user has entered the date of December so next three months date should be added.所以理想情况下应该计算 3 个月,即 1 月、2 月、3 月,因为用户输入了 12 月的日期,所以应该添加下三个月的日期。
SO How do I get the perfect number of days?那么如何获得完美的天数? Sorry, this question can feel so long or improper.
对不起,这个问题可能感觉太长或不合适。 but I am working on this from a week.
但我从一周开始就在做这个。 Note: I cannot use any external link, API, Library as this is a school project.
注意:我不能使用任何外部链接,API,图书馆,因为这是一个学校项目。
Any help will be great.任何帮助都会很棒。 also sorry for my English.
也对不起我的英语。 I'm not a native speaker.
我不是母语人士。
Let the Date
object do it for you, by using getFullyear
/ setFullYear
, getMonth
/ setMonth
, and getDate
/ setDate
.让
Date
object 为您完成,使用getFullyear
/ setFullYear
、 getMonth
/ setMonth
和getDate
/ setDate
。 They handle rollover and leap years:他们处理翻转年和闰年:
const yearsToAdd = 1; const monthsToAdd = 3; const daysToAdd = 0; const date = new Date(2020, 12 - 1, 10); console.log(date.toString()); date.setFullYear(date.getFullYear() + yearsToAdd); date.setMonth(date.getMonth() + monthsToAdd); date.setDate(date.getDate() + daysToAdd); console.log(date.toString());
Just as an example of handling leap years, here's that code adding two days to Feb 28th 2020 (which was a leap year, so there was a Feb 29th):作为处理闰年的一个例子,下面的代码在 2020 年 2 月 28 日(这是闰年,所以有 2 月 29 日)加上两天:
const yearsToAdd = 0; const monthsToAdd = 0; const daysToAdd = 2; const date = new Date(2020, 2 - 1, 28); console.log(date.toString()); date.setFullYear(date.getFullYear() + yearsToAdd); date.setMonth(date.getMonth() + monthsToAdd); date.setDate(date.getDate() + daysToAdd); console.log(date.toString());
Notice how it goes to March 1st, not March 2nd, as it does in 2019:请注意它是如何到 3 月 1 日的,而不是像 2019 年那样的 3 月 2 日:
const yearsToAdd = 0; const monthsToAdd = 0; const daysToAdd = 2; const date = new Date(2019, 2 - 1, 28); console.log(date.toString()); date.setFullYear(date.getFullYear() + yearsToAdd); date.setMonth(date.getMonth() + monthsToAdd); date.setDate(date.getDate() + daysToAdd); console.log(date.toString());
This is a duplicate of other questions:这是其他问题的重复:
To summarise some of the issues:总结一些问题:
There are other quirks with leap years and month lengths not mentioned above, such as 31 Aug + 1 month gives 1 Oct, because adding a month in August adds 31 days.上面没有提到闰年和月份长度的其他怪癖,例如 8 月 31 日 + 1 个月给出 10 月 1 日,因为在 8 月添加一个月会增加 31 天。
So you can either add business rules to deal with the quirks, or just let them happen.因此,您可以添加业务规则来处理这些怪癖,或者让它们发生。
In reagard to adding years, months and days, that can be done in one call to setFullYear :关于添加年、月和日,可以通过一次调用setFullYear来完成:
date.setFullYear(date.getFullYear() + yearsToAdd,
date.getMonth() + monthsToAdd,
date.getDate() + daysToAdd);
Adding them all at once can have a different result to adding them one at a time.一次添加它们可能会产生与一次添加它们不同的结果。
Given that you are getting values for the year, month, day, etc. then the values to add, you can simply add corresponding values (ensuring you convert strings to numbers before adding) and call the Date constructor once:假设您正在获取年、月、日等的值,然后是要添加的值,您可以简单地添加相应的值(确保在添加之前将字符串转换为数字)并调用 Date 构造函数一次:
let date = new Date(startYear + yearsToAdd,
startMonth - 1 + monthsToAdd, // Assuming user enters calendar month
startDay + daysToAdd,
startHour + hoursToAdd,
startMinute + minutesToAdd,
startSecond + secondsToAdd);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.