Skip to content
Advertisement

display current month until the 15th day, then to next month after the 15th day. E.g: If July 10 – display “July”, If July 20 – display “August”,

I’ve written script for both current month & following month (shown below). However, I am trying to achieve something in the middle; displaying the current month until the 15th day, then changing to next month after the 15th day.

Example: If today is July 10th, display “July”. If today is July 20th, display “August”.

Displaying the Current Month, alternating: “var n = month[d.getMonth()];”

{
  var month = new Array();
  month[0] = "January";
  month[1] = "February";
  month[2] = "March";
  month[3] = "April";
  month[4] = "May";
  month[5] = "June";
  month[6] = "July";
  month[7] = "August";
  month[8] = "September";
  month[9] = "October";
  month[10] = "November";
  month[11] = "December";

  var d = new Date();
  var n = month[d.getMonth()]; 

    document.write (month = n)
} 

Displaying the Following Month, alternating: “var n = month[d.getMonth()+1];”

{
  var month = new Array();
  month[0] = "January";
  month[1] = "February";
  month[2] = "March";
  month[3] = "April";
  month[4] = "May";
  month[5] = "June";
  month[6] = "July";
  month[7] = "August";
  month[8] = "September";
  month[9] = "October";
  month[10] = "November";
  month[11] = "December";

  var d = new Date();
  var n = month[d.getMonth()+1]; 

    document.write (month = n)
}

I’ve experimented with values in between the 0-1, but still no luck. I haven’t managed to find a solution after a lot of research so any help would be much appreciated.

Thanks in advance, Ryan.

Advertisement

Answer

You can get the day of the month with new Date().getDate(). Then, if it is more than 15, add 1 to the month index to return.

Also, I rewrote your months array in a simple form instead of month[0] = "January";

{
  var month = ["January","February","March","April","May","June","July","August","September","October","November","December"]

  var d = new Date();
  var dayOfMonth = d.getDate();
  var n = month[ d.getMonth() + (dayOfMonth > 15 ? 1 : 0) ]; 

    document.write ("month = " + n)
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement