Javascript 为未来的日期添加时间

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

Javascript Adding Time for a Future Date

javascriptdate

提问by Will Sam

I looked at some other questions, and don't see my specific problem, so please excuse me if it has been asked or answered.

我看了一些其他的问题,没有看到我的具体问题,所以如果有人问过或回答过,请原谅。

What I am trying to do is figure out a simple "payment" calculator, and provide some additional information, such as the first payment date, and the last.

我想要做的是找出一个简单的“付款”计算器,并提供一些附加信息,例如第一个付款日期和最后一个付款日期。

In some cases, the day of the last payment works, and sometimes it doesn't.

在某些情况下,最后一次付款的日期有效,有时则无效。

Here's my code:

这是我的代码:

var myDate = new Date();

var odo = document.contract.firstPaymentDate.value;
var n = odo.split("/"); 
var month = n[0];
var day = n[1];
var year = n[2];

var oldDateObj = new Date(year, month, day);
var newDateObj = new Date(oldDateObj.getTime() + ((document.contract.totalNumberRegularPayments.value - 1)*1209600*1000));
var dd = newDateObj.getDate();
var mm = newDateObj.getMonth();
var y = newDateObj.getFullYear();

var someFormattedDate = mm + '/'+ dd + '/'+ y;
document.contract.lastPaymentDate.value = someFormattedDate;

So I take the first payment date, and add 1209600 seconds times the number of payments (minus 1 since they have "already" paid the first).

所以我取第一个付款日期,加上 1209600 秒乘以付款次数(减去 1,因为他们“已经”支付了第一笔款项)。

This is based on starting at a specific day that can be chosen by the user.

这基于用户可以选择的特定日期开始。

So my example is 156 BiWeekly payments (so 155 for the calculations), which works out to 6 years. If I choose the date of the 1st, I get 10/01/2013 as the start, but 9/11/2019 as the end (First on a Tuesday, last on a Wednesday).

所以我的例子是 156 BiWeekly 付款(因此计算为 155),计算为 6 年。如果我选择 1 号的日期,我将 10/01/2013 作为开始,但 9/11/2019 作为结束(首先在星期二,最后在星期三)。

For the 15th (9/15/2013 - a Sunday - to 8/24/2019 - a Saturday) For the 20th (9/20/2013 - a Friday - to 8/29/2013 - a Thursday)

15日(9/15/2013-星期日-到8/24/2019-星期六) 20日(9/20/2013-星期五-到8/29/2013-星期四)

So since sometimes it's a day later, and sometimes a day ahead, I can't just +1 to var dd = newDateObj.getDate();

所以因为有时是一天后,有时是一天前,我不能只是 +1 到 var dd = newDateObj.getDate();

I'm really baffled as to what's going on, and I'm hoping someone out there either has some experience with this, or someone that knows what the heck I might be doing wrong.

我真的很困惑发生了什么,我希望那里的人要么有这方面的经验,要么知道我可能做错了什么。

Thanks in advance for any help you can offer.

预先感谢您提供的任何帮助。

回答by RobG

If you want to add a number of weeks, just add 7 times as many days, e.g.

如果你想添加几个星期,只需添加 7 倍的天数,例如

var now = new Date();
// Add two weeks
now.setDate(now.getDate() + 14);

So if you have 24 fortnightly payments:

因此,如果您每两周支付 24 笔款项:

var now = new Date();
// Add 24 fortnights (48 weeks)
now.setDate(now.getDate() + 24 * 14);

or

或者

now.setDate(now.getDate() + 24 * 2 * 7);

whatever you think is clearest.

你认为最清楚的。

If you want to have a start and end date:

如果你想有一个开始和结束日期:

var start = new Date();
var end = new Date(+start);
end.setDate(end.getDate() + 24 * 14);

alert('Start on: ' + start + '.\nEnd in 24 fortnights: ' + end); 

Edit

编辑

Here's a working example:

这是一个工作示例:

<script>

function calcLastPayment(start, numPayments) {
  if (typeof start == 'string') {
    start = stringToDate(start);
  }

  var end = new Date(+start);
  end.setDate(end.getDate() + --numPayments * 14)
  return end;
}

// Expect date in US format m/d/y
function stringToDate(s) {
  s = s.split(/\D/)
  return new Date(s[2], --s[0], s[1])
}

</script>

<form>
 <table>
  <tr><td>Enter first payment date (m/d/y):
      <td><input name="start">
  <tr><td>Enter number of payments:
      <td><input name="numPayments">
  <tr><td colspan="2"><input type="button" value="Calc end date" onclick="
           this.form.end.value = calcLastPayment(this.form.start.value, this.form.numPayments.value)
           ">
  <tr><td>Last payment date:
      <td><input readonly name="end">
  <tr><td colspan="2"><input type="reset">
 </table>
</form>

Given a first payment date of Thursday, 5 September and 3 repayments it returns Thursday 3 October, which seems correct to me (5 and 19 September and 3 October). It should work for any number of payments.

鉴于第一个付款日期是 9 月 5 日星期四和 3 次还款,它返回 10 月 3 日星期四,这对我来说似乎是正确的(9 月 5 日和 19 日以及 10 月 3 日)。它应该适用于任意数量的付款。

回答by Ryan Taylor

There is a simple way to do this in a line or two of code.

有一两行代码有一种简单的方法可以做到这一点。

I use 864e5for the number of milliseconds in a day. Because 1000 milliseconds/second * 60 seconds/minute * 60 minutes/hour * 24 hours/day = 86400000 milliseconds/dayor 864e5.

864e5用于一天中的毫秒数。因为1000 milliseconds/second * 60 seconds/minute * 60 minutes/hour * 24 hours/day = 86400000 milliseconds/day864e5

var now = new Date,
    day = 864e5,
    weekFromNow = new Date(+now + day * 7); //+now casts the date to an integer
  • +nowcasts the date to an integer, the number of milliseconds. now.valueOf()works too.
  • day * 7or 864e5 * 7is the number of milliseconds in a week.
  • new Date(...)casts the number of milliseconds to a date again.
  • +now将日期转换为整数,即毫秒数。 now.valueOf()也有效。
  • day * 7或者864e5 * 7是一周中的毫秒数。
  • new Date(...)再次将毫秒数转换为日期。

Sometimes you don't have to worry about casting the value back to a date.

有时您不必担心将值转换回日期。