Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q170 How do you add a number of months to a date and then format the output?

You are here: irt.org | FAQ | JavaScript | Date | Q170 [ previous next ]

<SCRIPT LANGUAGE="JavaScript"><!--
function makeArray() {
    for (i = 0; i<makeArray.arguments.length; i++)
        this[i + 1] = makeArray.arguments[i];
}

var months = new makeArray('January','February','March','April',
                           'May','June','July','August','September',
                           'October','November','December');

function nths(day) {
    if (day == 1 || day == 21 || day == 31) return 'st';
    if (day == 2 || day == 22) return 'nd';
    if (day == 3 || day == 23) return 'rd';
    return 'th';
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function monthsahead(noofmonths) {
    var today = new Date();
    var date = new Date(today.getYear(),today.getMonth() + noofmonths,today.getDate(),today.getHours(),today.getMinutes(),today.getSeconds());
    return date.getDate() + nths(date.getDate()) + ' ' + months[date.getMonth() + 1] + ' ' + y2k(date.getYear());
}

document.write(monthsahead(6));
//--></SCRIPT>

The following was submitted anonymously:

I tried the answer to FAQ1 70 and found it to be severely flawed. This enhancement adds months, days, or years and includes sample test scenarios. Copy it into an .html file and double click to run the test scenarios.

<script Language="JavaScript">
function makeArray() {
  for (i = 0; i<makeArray.arguments.length; i++)
    this[i + 1] = makeArray.arguments[i];
}

var months = new makeArray('January','February','March','April',
                           'May','June','July','August','September',
                           'October','November','December');

function nths(day) {
    if (day == 1 || day == 21 || day == 31) return 'st';
    if (day == 2 || day == 22) return 'nd';
    if (day == 3 || day == 23) return 'rd';
    return 'th';
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function daysInMonth(theYear, theMonth) {
    var numDays = 27;
    var refMonth = --theMonth;
    while (refMonth == theMonth) {
      var refDate = new Date(theYear, theMonth, ++numDays, 0, 0, 0, 0);
      refMonth = refDate.getMonth();
    }
    return --numDays;
}

function addDaysToDate(myDate,days) {
    return new Date(myDate.getTime() + days*24*60*60*1000);
}

function addMonthsToDate(startDate, numMonths) {
    var addYears = Math.floor(numMonths/12);
    var addMonths = numMonths - (addYears * 12);
    var newMonth = startDate.getMonth() + addMonths;
    if (startDate.getMonth() + addMonths > 11) {
      ++addYears;
      newMonth = startDate.getMonth() + addMonths - 12;
    }
    var newDate = new Date(startDate.getYear()+addYears,newMonth,startDate.getDate(),startDate.getHours(),startDate.getMinutes(),startDate.getSeconds());

    // adjust to correct month
    while (newDate.getMonth() != newMonth) {
      newDate = addDaysToDate(newDate, -1);
    }

    return newDate;
}

function addDuration(startDate, durationUnit, durationValue) {
  var numMonths;
  if (durationUnit == "YEAR")
    numMonths = durationValue * 12;
  else
    if (durationUnit == "MONTH")
      numMonths = durationValue;
    else
      if (durationUnit == "DAY") {
        return addDaysToDate(startDate, durationValue);
      }
      else
       return startDate();   // invalid durationUnit
  return addMonthsToDate(startDate, numMonths);
}

function isValidDate(year, month, day) {
  checkDate = new Date(year, --month, day, 0, 0, 0, 0);
  return checkDate.getMonth() == month;
}

function formatDate(aDate) {
  return aDate.getDate() + nths(aDate.getDate()) + ' ' + months[aDate.getMonth() + 1] + ' ' + y2k(aDate.getYear());
}

function testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit) {
  document.writeln("Scenario " + scenario);
  document.writeln("DurationValue " + durationValue.toString());
  document.writeln("DurationUnit " + durationUnit.toString());
  if (!isValidDate(startYear, startMonth, startDay)) {
    document.writeln("Invalid start date " + startYear + "/" + (startMonth) + "/" + startDay);
  }
  else {
    document.writeln("valid date");
    startDate = new Date(startYear, startMonth-1, startDay, 0, 0, 0, 0);
    document.writeln(formatDate(startDate));
    endDate = addDuration(startDate, durationUnit, durationValue);
    document.writeln(formatDate(endDate));
  }
  document.writeln();
}

function testLastActive() {

  var scenario;
  var startDate;
  var endDate;
  var startYear;
  var startMonth;
  var startDay;
  var durationValue;
  var durationUnit;

  scenario=1;
  startYear=2003;
  startMonth=12;
  startDay=31;
  durationValue=1;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=2;
  startYear=2003;
  startMonth=12;
  startDay=31;
  durationValue=2;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=3;
  startYear=2003;
  startMonth=12;
  startDay=31;
  durationValue=6;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=4;
  startYear=2003;
  startMonth=12;
  startDay=31;
  durationValue=11;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=5;
  startYear=2003;
  startMonth=12;
  startDay=31;
  durationValue=12;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=6;
  startYear=2003;
  startMonth=12;
  startDay=31;
  durationValue=14;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=1;
  startYear=2003;
  startMonth=12;
  startDay=31;
  durationValue=26;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=7;
  startYear=2003;
  startMonth=12;
  startDay=31;
  durationValue=1;
  durationUnit="YEAR";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=8;
  startYear=2003;
  startMonth=12;
  startDay=31;
  durationValue=3;
  durationUnit="YEAR";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=9;
  startYear=2003;
  startMonth=2;
  startDay=28;
  durationValue=2;
  durationUnit="YEAR";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=10;
  startYear=2003;
  startMonth=2;
  startDay=29;
  durationValue=2;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=11;
  startYear=2002;
  startMonth=1;
  startDay=1;
  durationValue=1;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=12;
  startYear=2002;
  startMonth=1;
  startDay=1;
  durationValue=2;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=13;
  startYear=2002;
  startMonth=2;
  startDay=28;
  durationValue=2;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=14;
  startYear=2002;
  startMonth=6;
  startDay=31;
  durationValue=2;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=15;
  startYear=2002;
  startMonth=7;
  startDay=31;
  durationValue=2;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=16;
  startYear=2004;
  startMonth=2;
  startDay=29;
  durationValue=1;
  durationUnit="YEAR";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=17;
  startYear=2002;
  startMonth=2;
  startDay=28;
  durationValue=3;
  durationUnit="DAY";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=18;
  startYear=2002;
  startMonth=10;
  startDay=31;
  durationValue=3;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=19;
  startYear=2002;
  startMonth=12;
  startDay=01;
  durationValue=3;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=20;
  startYear=2002;
  startMonth=12;
  startDay=31;
  durationValue=12;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=21;
  startYear=2002;
  startMonth=12;
  startDay=31;
  durationValue=14;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=22;
  startYear=2002;
  startMonth=01;
  startDay=10;
  durationValue=1;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=23;
  startYear=2002;
  startMonth=01;
  startDay=20;
  durationValue=1;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=24;
  startYear=2002;
  startMonth=12;
  startDay=31;
  durationValue=1;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=25;
  startYear=2002;
  startMonth=12;
  startDay=31;
  durationValue=2;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=26;
  startYear=2002;
  startMonth=12;
  startDay=31;
  durationValue=6;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=27;
  startYear=2002;
  startMonth=12;
  startDay=31;
  durationValue=11;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=28;
  startYear=2002;
  startMonth=08;
  startDay=15;
  durationValue=12;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=29;
  startYear=2002;
  startMonth=07;
  startDay=31;
  durationValue=14;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=30;
  startYear=2002;
  startMonth=12;
  startDay=31;
  durationValue=26;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=31;
  startYear=2002;
  startMonth=12;
  startDay=31;
  durationValue=1;
  durationUnit="YEAR";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=32;
  startYear=2002;
  startMonth=12;
  startDay=31;
  durationValue=3;
  durationUnit="YEAR";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=33;
  startYear=2002;
  startMonth=2;
  startDay=28;
  durationValue=2;
  durationUnit="YEAR";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

  scenario=34;
  startYear=2002;
  startMonth=2;
  startDay=29;
  durationValue=2;
  durationUnit="MONTH";
  testScenario(scenario,startDate,endDate,startYear,startMonth,startDay,durationValue,durationUnit);

}

testLastActive();
</script>

Feedback on 'Q170 How do you add a number of months to a date and then format the output?'

©2018 Martin Webb