Turning an Integer Value into a Date Using that Integer and the Current Month

时间:2018-08-22 13:43:29

标签: javascript date

I have a function in my MongoDB/Node backend where I calculate a nextPaymentDate based on a combination of the current date and value(s) in a field (which is an array) titled paymentDateInts.

I have some logic that determines what is the correct integer of these two to assign to nextPaymentDateInt. This looks like this:

  let currentDate = new Date();
  let currentDateInt = currentDate.getDate();
  let paymentDateInts = [1, 15];
  let firstDateInt = paymentDateInts[0];
  let secondDateInt = paymentDateInts[1];

  let nextPaymentDateInt;

  if (firstDateInt < currentDateInt) {
    nextPaymentDateInt = firstDateInt;
  } else {
    nextPaymentDateInt = secondDateInt;
  }

However, I want nextPaymentDate to be a date, not an integer. So how do I take the integer returned from the above function, and then turn that into a date.

UPDATE:

It just occurred to me that this is a little more complicated than I first assumed. Because I need to generate a date using that integer and either the current month, or the following month - whichever should apply.

In other words, if the second value is 15, and today is the 10th, then I should get a date with the current month and the integer 15. However, if today's date is beyond that 15th, then the date calculated should be for the next month and the integer 1.

4 个答案:

答案 0 :(得分:1)

You can use the new Date(year, monthIndex, day); constructor to construct your Date object

let nextPaymentDate = new Date(d.getFullYear(), d.getMonth(), nextPaymentDateInt);

Note:

There's no need to declare useless variables such as paymentDateInts, firstDateInt and secondDateInt, just compare the currentDateInt with 15 and construct your Date accordingly:

let currentDate = new Date();
let currentDateInt = currentDate.getDate();

let nextPaymentDate;

if (15 >= currentDateInt) {
  nextPaymentDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDateInt);
} else {
  nextPaymentDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 1);
}

Demo:

let currentDate = new Date();
let currentDateInt = currentDate.getDate();

var nextPaymentDate;

if (15 >= currentDateInt) {
  nextPaymentDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDateInt);
} else {
  nextPaymentDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 1);
}
console.log(nextPaymentDate);

答案 1 :(得分:1)

正如chŝdk所说,您可以用更少的代码来编写此代码,并且只需要一个Date。只需使用paymentDateInts[1]检查日期,然后相应地设置值即可:

var paymentDateInts = [1, 15];
var d = new Date();

if (d.getDate() < paymentDateInts[1]) {
  d.setDate(paymentDateInts[1]);

} else {
  d.setMonth(d.getMonth() + 1, paymentDateInts[0]);
}

console.log(d.toString());

答案 2 :(得分:0)

If you want toget a date at the same month then use getFullYear(), getMonth() when creating a new Date:

let nextPaymentDate = new Data(currentDate.getFullYear(), currentDate.getMonth(), nextPaymentDateInt)

Else, if you need next month, then use currentDate.getMonth() + 1

答案 3 :(得分:0)

You can use the setDate method. For example

let firstDateInteger = new Date();
firstDateInteger.setDate(paymentDateInts[0]);