简体   繁体   English

如何将日期值转换为天数

[英]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 / setFullYeargetMonth / setMonthgetDate / 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:这是其他问题的重复:

  1. Add A Year To Today's Date 为今天的日期添加一年
  2. JavaScript function to add X months to a date JavaScript function 将 X 个月添加到日期
  3. Add days to JavaScript Date 将日期添加到 JavaScript 日期

To summarise some of the issues:总结一些问题:

  1. Days are not always 24 hours long where daylight saving occurs, so you must determine whether "adding 1 day" means adding 1 calendar day or adding 24 hours夏令时发生的日子并不总是 24 小时,因此您必须确定“增加 1 天”是指增加 1 个日历日还是增加 24 小时
  2. Months are 28 to 31 days long, so adding 1 month adds a different number of days depending on the month you're adding to月份为 28 到 31 天,因此添加 1 个月会增加不同的天数,具体取决于您要添加的月份
  3. Years are 365 or 366 days long.年份是 365 或 366 天。 In a leap year, adding 1 year to 29 Feb adds 366 days and ends on 2 Mar. The following day, adding 1 year to 1 Mar ends on 1 Mar闰年,2 月 29 日加 1 年,366 天,3 月 2 日结束。次日,3 月 1 日加 1 年,3 月 1 日结束

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.

相关问题 如何将最初为日期的序列号转换为日期最初具有的天数但不使用日期 object? - How to convert a serial number that was initially a date to the number of days that the date initially had but without using the date object? 将日期转换为天数然后执行操作 - convert date to number of days then do stuff 如何将日期转换为时间戳,或者最好如何将 n 天数添加到 javascript 中的时间戳 - How can i convert date to timestamp or preferably how can i add n number of days to a timestamp in javascript 如何使用“-”将日期作为数字转换为日期 - How to convert Date as number to Date with "-" 如何将公历日转换为公历日期 - How to convert gregorian days to gregorian date 如何通过在javascript中给出天数来增加日期 - how to increment date by giving the number of days in javascript 如何将字符串 eg: '8/31/2020' 转换为日期,并从今天的日期中减去它以查找 JavaScript 中已经过去的天数 - How to convert a string eg: '8/31/2020' to a date, and substract it from todays date to find number of days that have passed in JavaScript 如何在日期范围内的 n 天后获取日期? - How to get date after n number of days in a date range? 如何获取当前日期和提供的日期之间的天数 - how to get the number of days between the current date and the date supplied jquery 将天数转换为年、月、日 - Convert number of days into years, months, days
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM