Dynamically write the current month -1 name to a text box with logic to handle January

// Get today's date
var today = new Date();

// Define array of month names
var monthNames = ["January", "February", "March", "April", "May", "June",
                  "July", "August", "September", "October", "November", "December"];

// Get current month index (0 = January, 11 = December)
var currentMonthIndex = today.getMonth();

// Declare variable for last month index
var lastMonthIndex = 0;

// Use if/else instead of unsupported ternary operator
if (currentMonthIndex === 0) {
    lastMonthIndex = 11;
} else {
    lastMonthIndex = currentMonthIndex - 1;
}

// Get the name of the previous month
var lastMonthName = monthNames[lastMonthIndex];

// Write result to console
console.log(lastMonthName);
Text_Box.applyText("Data set to current month -1:"+" "+"("+lastMonthName+")");

Code language: JavaScript (javascript)
Scroll to Top