简体   繁体   English

Javascript为未来日期添加时间

[英]Javascript Adding Time for a Future Date

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. 所以我的例子是156 BiWeekly付款(因此计算为155),这可以达到6年。 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). 如果我选择第一天的日期,我将以2013年10月1日作为开始,但是以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届(2013年9月15日 - 周日 - 至2009年8月24日 - 周六)20日(2013年9月20日 - 周五至2013年8月29日 - 周四)

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. 提前感谢您提供的任何帮助。

If you want to add a number of weeks, just add 7 times as many days, eg 如果您想要添加几周,只需添加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). 鉴于第一个付款日期为9月5日星期四和3月还款,它将于10月3日星期四返回,这对我来说是正确的(9月5日和19日以及10月3日)。 It should work for any number of payments. 它适用于任何数量的付款。

There is a simple way to do this in a line or two of code. 有一种简单的方法可以在一行或两行代码中完成此操作。

I use 864e5 for the number of milliseconds in a day. 我使用864e5作为一天中的毫秒数。 Because 1000 milliseconds/second * 60 seconds/minute * 60 minutes/hour * 24 hours/day = 86400000 milliseconds/day or 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
  • +now casts the date to an integer, the number of milliseconds. +now将日期转换为整数,毫秒数。 now.valueOf() works too. now.valueOf()也可以。
  • day * 7 or 864e5 * 7 is the number of milliseconds in a week. day * 7864e5 * 7是一周内的毫秒数。
  • new Date(...) casts the number of milliseconds to a date again. new Date(...)再次将日期数转换为日期。

Sometimes you don't have to worry about casting the value back to a date. 有时您不必担心将值转换回日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM