如何使用 javascript 添加星期至今?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11343939/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:33:12  来源:igfitidea点击:

How to add weeks to date using javascript?

javascriptdate-format

提问by JBoom

Javascript definitely isn't my strongest point. I've been attempting this for a couple of hours now and seem to be getting stuck with date formatting somewhere.

Javascript 绝对不是我的强项。我已经尝试了几个小时,似乎在某处陷入了日期格式的困境。

I have a form where a user selected a date (dd/mm/yyyy) and then this date will be taken and 2 weeks will be added to it and then date will be copied to another form field.

我有一个表单,其中用户选择了一个日期 (dd/mm/yyyy),然后将采用该日期并将 2 周添加到其中,然后将日期复制到另一个表单字段。

My latest attempt below isn't even adding a date yet just copying the selected date in one form field to another, if I select '03/02/2012', it outputs 'Fri Mar 02 2012 00:00:00 GMT+0000 (GMT Standard Time)', so its outputting in American format as well as the full date. How to I get it to out put in the same format and add 2 weeks?

我在下面的最新尝试甚至没有添加日期,只是将一个表单字段中的选定日期复制到另一个,如果我选择“03/02/2012”,它会输出“Fri Mar 02 2012 00:00:00 GMT+0000” (GMT 标准时间)',因此它以美国格式和完整日期输出。如何让它以相同的格式发布并增加 2 周?

function LicenceToOccupy(acceptCompletionDate)
{
    var date1 = new Date(acceptCompletionDate);
    document.frmAccept.acceptLicence.value = date1;

}

回答by Arvind Sridharan

You can do this :

你可以这样做 :

let numWeeks = 2;
let now = new Date();
now.setDate(now.getDate() + numWeeks * 7);
alert(now);

You can see the fiddle here.

你可以在这里看到小提琴。

According to the documentation in MDN

根据MDN 中文档

The setDate() method sets the day of the Date object relative to the beginning of the currently set month.

setDate() 方法设置 Date 对象相对于当前设置月份的开始日期。

回答by Luis

This might not answer the question per se, but one can find a solution with these formulas.

这本身可能无法回答问题,但可以使用这些公式找到解决方案。

6.04e+8= 1 week in milliseconds

6.04e+8= 1 周(以毫秒为单位)

Date.now()= Now in milliseconds

Date.now()= 现在以毫秒为单位

Date.now() + 6.04e+8= 1 week from today

Date.now() + 6.04e+8= 从今天起 1 周

Date.now() + (6.04e+8 * 2)= 2 weeks from today

Date.now() + (6.04e+8 * 2)= 从今天起 2 周

new Date( Date.now() + (6.04e+8 * 2) )= Date Object for 2 weeks from today

new Date( Date.now() + (6.04e+8 * 2) )= 从今天起 2 周的日期对象

回答by Matt

You're assigning date1to be a Date object which representsthe string you pass it. What you're seeing in the acceptLicensevalue is the toString()representation of the date object (try alert(date1.toString())to see this).

您正在分配date1一个 Date 对象,它代表您传递给它的字符串。你在acceptLicense值中看到的是toString()日期对象的表示(试着alert(date1.toString())看看这个)。

To output as you want, you'll have to use string concatenation and the various Datemethods.

要根据需要输出,您必须使用字符串连接和各种Date方法

var formattedDate = date1.getDate() + '/' + (date1.getMonth() + 1) + '/' + date1.getFullYear();

In terms of adding 2 weeks, you should add 14 days to the current date;

在增加2周方面,您应该在当前日期上增加14天;

date1.setDate(date.getDate() + 14);

... this will automatically handle the month increase etc.

...这将自动处理月份增加等。

In the end, you'll end up with;

最后,你会得到;

var date1 = new Date(acceptCompletionDate);
date1.setDate(date1.getDate() + 14);
document.frmAccept.acceptLicence.value = date1.getDate() + '/' + (date1.getMonth() + 1) + '/' + date1.getFullYear();

N.BMonths in JavaScript are 0-indexed (Jan = 0, Dec = 11), hence the +1on the month.

JavaScript 中的NB月份是 0 索引的(Jan = 0,Dec = 11),因此 +1在月份。

Edit: To address your comment, you should construct dateas follows instead, as the Dateargument is supposed to be "A string representing an RFC2822 or ISO 8601 date." (see here).

编辑:为了解决您的评论,您应该构造date如下,因为Date参数应该是“表示 RFC2822 或 ISO 8601 日期的字符串”。(见这里)。

var segments = acceptCompletionDate.split("/");
var date1 = new Date(segments[2], segments[1], segments[0]);

回答by Anshul Jain

var d = new Date("2019-08-01");
d.setDate(d.getDate()+parseInt(7));

here 7 is the days which you want to add in the date

这里 7 是您要添加到日期中的天数

回答by Me.Name

To parse the specific dd/mm/yyyyformat and increment days with 14 , you can do something like split the parts, and create the date object with y/m/d given specfically. (incrementing the days right away) Providing the separator is always -, the following should work:

要解析特定dd/mm/yyyy格式并使用 14 增加天数,您可以执行诸如拆分部分之类的操作,并使用特定给定的 y/m/d 创建日期对象。(立即增加天数)如果分隔符始终为-,则以下内容应该有效:

function LicenceToOccupy(acceptCompletionDate)
{
    var parts = acceptCompletionDate.split("/");
    var date1 = new Date(parts[2], (parts[1] - 1), parseInt(parts[0]) + 14); //month 0 based, day: parse to int and increment 14 (2 weeks)
    document.frmAccept.acceptLicence.value = date1.toLocaleDateString(); //if the d/m/y format is the local string, otherwise some cusom formatting needs to be done

}

回答by Franklin Rivero

Just some minimum modifications to Toast's answer and you will get the exact format you are looking for dd/mm/yyyy:

只需对 Toast 的答案进行一些最小修改,您将获得您正在寻找的确切格式dd/mm/yyyy

function LicenceToOccupy(acceptCompletionDate)
{
    var date1 = new Date(acceptCompletionDate);
    date1.setDate(date1.getDate() + 14); //This adds the two weeks

    var day = date1.getDate() 9 10 ? date1.getDate() : '0' + date1.getDate();
    var month = date1.getMonth() >= 9 ? date1.getMonth() + 1: '0' + date1.getMonth();

    document.frmAccept.acceptLicence.value = day + '/' + month + '/' + date1.getFullYear();
}

回答by Toast

This should do what you're looking for.

这应该做你正在寻找的。

function LicenceToOccupy(acceptCompletionDate)
{
    var date1 = new Date(acceptCompletionDate);
    date1.setDate(date1.getDate() + 14);
    document.frmAccept.acceptLicence.value = date1.getDate() + '/' + (date1.getMonth() + 1) + '/' + date1.getFullYear();
}

回答by Ricola3D

Everything's there!

一切都在那里

 date1.toLocaleDateString() 

Thiswill return you date1 as a String in the client convention

这将在客户端约定中将 date1 作为字符串返回

To create a new date date2 with 2 weeks more (2weeks = 2*7*24*60*60 seconds):

要创建一个多 2 周的新日期 date2(2 周 = 2*7*24*60*60 秒):

 var date2 = new Date(date1 + 60*60*24*7*2);