0

Hi I am trying to add 31 days to 'myDate' which is the current date. It is supposed to get the date add 31 days, then the convertDate function is supposed to translate it to something like 'Nov 31, 2012'. But it doesn't work. Does anyone know why?

Here is the primary function...

function process (infoarray) {
  var myDate = new Date();
  //var final = convertDate(myDate);

  var length = infoarray.length;
  var final_string;
  for (var b = 0; b < length; b++) {
    if (b == 0) {
      if (infoarray[b][3] == 'After') {
        final_string = '<b>' + infoarray[b][3] + '&nbsp;' + infoarray[b][1] + '</b><br/>' + infoarray[b][0] + '<br/>';
      } else {
        final_string = '<b>' + infoarray[b][1] + '&nbsp;' + infoarray[b][3] + '&nbsp;' + infoarray[b][2] + '</b><br/>' + infoarray[b][0] + '<br/>';
      }
    } else {
      if (infoarray[b][3] == 'After') {

        final_string = final_string + '<br/><b>' + infoarray[b][3] + '&nbsp;' + convertDate(myDate.setDate(myDate.getDate() + 31)) + '</b><br/>' + infoarray[b][0] + '<br/>';
      } else {
        final_string = final_string + '<br/><b>' + infoarray[b][1] + '&nbsp;' + infoarray[b][3] + '&nbsp;' + infoarray[b][2] + '</b><br/>' + infoarray[b][0] + '<br/>';
      }
    }
  }
  return final_string;
}

Here is the line i am focused on from the function above...

final_string = final_string + '<br/><b>' + infoarray[b][3] + '&nbsp;' + convertDate(myDate.setDate(myDate.getDate() + 31)) + '</b><br/>' + infoarray[b][0] + '<br/>';

Here is the convertDate function...

function convertDate(d) {
    var day = d.getDate();
    if (day < 10) {
        day = '0' + day;
    }
    var year = d.getFullYear();
    var month = d.getMonth();
    var months=['Jan','Feb','Mar','Apr','May','June','July','Aug','Sep','Oct', 'Nov','Dec'];
    var currentMonth = months[month];
    return (currentMonth + ' ' + day + ', ' + year);
}
1
  • 2
    Why doesn't it work? Any error message in the console? What is the output? Commented Oct 30, 2012 at 15:08

3 Answers 3

2

myDate.setDate(...) modifies the value of the Date instance, but doesn't return anything.
You need to call setDate first, then pass the variable to your function.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried this myDate.setDate(convertDate(myDate.getDate() + 31)) but it didn't work either..
do you know what the fix would look like?
convertDate(myDate.setDate(myDate.getDate() + 31)) to myDate.setDate(myDate.getDate() + 31); convertDate(myDate)
1

Here. First calculate then call

DEMO;

var myDate = new Date();
myDate.setDate(myDate.getDate()+31);

final_string = final_string + '<br/><b>' +
infoarray[b][3] + '&nbsp;' + 
convertDate(myDate) + '</b><br/>' + infoarray[b][0] + '<br/>';

Or add it to the function:

.... convertDate(myDate,31) + ....

With

function convertDate(d,offset) {
 if ( offset ) d.setDate(d.getDate()+offset);
 var day = d.getDate();

 if (day < 10) {
    day = '0' + day;
 }
 var year = d.getFullYear();
 var month = d.getMonth();
 var months=['Jan','Feb','Mar','Apr','May','June','July','Aug','Sep','Oct', 'Nov','Dec'];
 var currentMonth = months[month];
 return (currentMonth + ' ' + day + ', ' + year);
}

DEMO

6 Comments

I need to be able to use myDate as the current date over and over again..So I don't want to permanently change it.
Therefore in this specific part of the code I pointed out I need to get the current date add any number (ex: 31) to it and return it
I tried adding this but it didn't work... convertDate(infoarray[b][1].setDate(myDate.getDate() + 31))
So add it in convert date function convertDate(d,offset) { if ( offset ) d.setDate(d.getDate()+offset); var day = d.getDate();
Can you display what the convertDate function would look like?
|
0

By using setDate, you tell the Date object to set the day number to something between 32 and 62, which doesn't make much sense.

A good way to add 31 days would be to use getTime, which returns the number of mseconds ellapsed since 1st Jan 1970:

myDate.setTime( myDate.getTime()+31*24*60*60*1000 );
//31Days x 24 hours x 60 minutes x 60 seconds x 1000 msecs

4 Comments

I see..how would I apply the convertDate function to this too?
No need. Setting a date to 62 will add 31 days to the 31st so will work
well, mdn fooled me (developer.mozilla.org/en-US/docs/JavaScript/Reference/…) . I checked up ecma-international.org/ecma-262/5.1/#sec-15.9.1.12 and you're right. the argument passed to setDate isn't constrained
I do do see where this is fooling you "If the parameter you specify is outside of the expected range, setDate attempts to update the date information in the Date object accordingly. For example, if you use 0 for dayValue, the date will be set to the last day of the previous month."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.