Javascript为日期增加了天数

时间:2022-08-25 17:30:30

Hi I am trying to create a variable today that is the current date today. I am trying to add 106 days to it which works successfully. Then I am trying to create a second variable today2 and subtract 31 days from the 'today' variable (current date + 106 -31). This part is not working. This is what it is giving me...

嗨,我今天想创建一个变量,这是今天的日期。我正在尝试给它增加106天,这是成功的。然后我尝试创建第二个变量today2,并从“today”变量中减去31天(当前日期+ 106 -31)。这部分不工作。这就是它给我的……

Thu Mar 28 11:52:21 EDT 2013
Tue Nov 27 11:52:21 EST 2012

美国东部时间2013年11月28日11:52:21星期二美国东部时间2012年11月27日11:52:21

The second line is not 31 days before the first line. Can someone help me correct this?

第二条线离第一条线还不到31天。有人能帮我改正吗?

Feel free to play with my jsfiddle http://jsfiddle.net/fjhxW/

请随意使用我的jsfiddle http://jsfiddle.net/fjhxW/

<div id="current"></div>
<div id="current2"></div>
<div id="current3"></div>

var today = new Date();
var today2 = new Date();

today.setDate(today.getDate() + 106);

today2.setDate(today.getDate() - 31);  

var dd = today.getDate();
var mm = today.getMonth(); //January is 0!
var yy = today.getFullYear();

document.getElementById('current').innerHTML = today;
document.getElementById('current2').innerHTML = today2;

4 个解决方案

#1


4  

it's Xmas time so I give the answer just to copy/paste:

现在是圣诞节,所以我给出的答案只是复制/粘贴:

var oneDay = 24 * 60 * 60 * 1000, // 24h
    today = new Date().getTime(), // in ms
    firstDate,
    secondDate;

firstDate = new Date(today + 106 * oneDay);
secondDate = new Date(firstDate.getTime() - 31 * oneDay);

#2


2  

try datejs:

试试datejs:

Date.parse('t - 31 d'); // today - 31 days
Date.today().add(106).days().add(-31).days();

#3


0  

You cannot pass a negative number to setDate. setDate is used to set the date to set the absolute day, not relative days.

不能将负数传递给setDate。setDate用于设置日期,以设置绝对日,而不是相对日。

From the docs:

从文档:

If the parameter you specify is outside of the expected range, setDate attempts to update the date information in the Date object accordingly. For example, if you use 0 for dayValue, the date will be set to the last day of the previous month.

如果指定的参数超出预期范围,则setDate将尝试相应地更新日期对象中的日期信息。例如,如果您使用0作为dayValue,日期将设置为前一个月的最后一天。

#4


0  

A mathemathical solution:

一个mathemathical解决方案:

Add 75 days to your current day (106 - 31), then add 31 days to that date. Change the order in what you are showing both dates on your code.

在你现在的一天中增加75天(106 - 31),然后在那个日期增加31天。更改代码中显示的两个日期的顺序。

Why go forward and backward when you can always go forward?

当你总是可以前进的时候,为什么要前进和后退呢?

#1


4  

it's Xmas time so I give the answer just to copy/paste:

现在是圣诞节,所以我给出的答案只是复制/粘贴:

var oneDay = 24 * 60 * 60 * 1000, // 24h
    today = new Date().getTime(), // in ms
    firstDate,
    secondDate;

firstDate = new Date(today + 106 * oneDay);
secondDate = new Date(firstDate.getTime() - 31 * oneDay);

#2


2  

try datejs:

试试datejs:

Date.parse('t - 31 d'); // today - 31 days
Date.today().add(106).days().add(-31).days();

#3


0  

You cannot pass a negative number to setDate. setDate is used to set the date to set the absolute day, not relative days.

不能将负数传递给setDate。setDate用于设置日期,以设置绝对日,而不是相对日。

From the docs:

从文档:

If the parameter you specify is outside of the expected range, setDate attempts to update the date information in the Date object accordingly. For example, if you use 0 for dayValue, the date will be set to the last day of the previous month.

如果指定的参数超出预期范围,则setDate将尝试相应地更新日期对象中的日期信息。例如,如果您使用0作为dayValue,日期将设置为前一个月的最后一天。

#4


0  

A mathemathical solution:

一个mathemathical解决方案:

Add 75 days to your current day (106 - 31), then add 31 days to that date. Change the order in what you are showing both dates on your code.

在你现在的一天中增加75天(106 - 31),然后在那个日期增加31天。更改代码中显示的两个日期的顺序。

Why go forward and backward when you can always go forward?

当你总是可以前进的时候,为什么要前进和后退呢?