0

Does anyone know how to always set a date to 28th day of current month, or 28th day of the next month if date has passed 28th day of current month, and then calculate new date using variable (number of months). This is how it was done in .NET:

DateSerial(Year(DateAdd("m",
AmountOfMonths,
CDate(Invoice_date))),
Month(DateAdd(DateInterval.Month,
AmountOfMonths, CDate(Invoice_date))), 28)

What I tried so far:

var currentDate = new Date();
var day = currentDate.getDate() ;
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();
if  (day <= "28") result += day ="28";
else result += day ="28";
if  (day > "28") result = month + "1";
results.html("<b>" + year + "/" + month + "/" + day + "</b>");

I already have a problem with setting next month if day is 29, 30 or 31. Then I need to count new date by adding months (5, 7 or 15 or just any number).

Goran Kutlaca
  • 2,014
  • 1
  • 12
  • 18
Greg
  • 1
  • 2

3 Answers3

0

You can try this using toLocaleDateString() and javascript Date object getter & setter methods:

// Returns a string in mm/dd/yyyy format
const GetFormattedDate = d => d.toLocaleDateString('en-US');

const CalculateDate = (dateStr) => {
  var currentDate = dateStr ? new Date(dateStr) : new Date();

  // Log the current actual date
  console.log('Actual date: ', GetFormattedDate(currentDate));

  var day = currentDate.getDate();
  if (day > 28) {
    currentDate.setMonth(currentDate.getMonth() + 1);
  }
  currentDate.setDate(28);

  // Return 28th of current or next month
  return GetFormattedDate(currentDate);
}

// 1 week ago
console.log('Updated date: ', CalculateDate('02/21/2020'))

// Today
console.log('Updated date: ', CalculateDate())

// 1 week later
console.log('Updated date: ', CalculateDate('03/06/2020'))

// 1 month later
console.log('Updated date: ', CalculateDate('03/29/2020'))

// 1 year later
console.log('Updated date: ', CalculateDate('02/21/2021'))
.as-console-wrapper { max-height: 100% !important; top: 0; }
palaѕн
  • 72,112
  • 17
  • 116
  • 136
0

To add a month, add one to the month in the Date. You only need to do something if the date is greater than 28, e.g.

var currentDate = new Date();
var currentDay = currentDate.getDate() ;
currentDate.setDate(28);

if  (currentDay > 28) {
  currentDate.setMonth(currentDate.getMonth() + 1);
}

var month = currentDate.getMonth() + 1;
var year  = currentDate.getFullYear();

console.log(year + '/' + month + '/' + 28);
RobG
  • 142,382
  • 31
  • 172
  • 209
  • This worked fine for me :)var field = ""; field = record.fields["CreditTime2"]; var currentDate = new Date(); var currentDay = currentDate.getDate() ; currentDate.setDate(28); if (currentDay > 28) { currentDate.setMonth(currentDate.getMonth() + 1); } //add number of months to already calculated date currentDate.setMonth(currentDate.getMonth() + parseInt(field)); var month = currentDate.getMonth() + 1; var year = currentDate.getFullYear(); results.html(year + '-' + month + '-' + 28); – Greg Mar 09 '20 at 09:57
  • @Greg—don't post code in comments, put them in an answer. If one of the answers answers your question, accept it (including your own) so others don't think you're still looking for an answer. If you wish to modify your question, then do that. Don't modify it in your answer. – RobG Mar 09 '20 at 13:13
0

Here is code, that works for me! Thankseveryone for helping me out!

var field = "";
field = record.fields["CreditTime2"];
var currentDate = new Date();
var currentDay = currentDate.getDate() ;
currentDate.setDate(28);
if (currentDay > 28) { currentDate.setMonth(currentDate.getMonth() + 1);
}
//add number of months to already calculated date
currentDate.setMonth(currentDate.getMonth() + parseInt(field));
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();
results.html(year + '-' + month + '-' + 28); 
Greg
  • 1
  • 2