Current Quarter of a year
If the current quarter of the year (e.g., Q1) is required, the provided JavaScript Nashorn code can be used.
It uses the current month as its input and returns the corresponding quarter: Q1, Q2, Q3, or Q4. In case of invalid input, it returns a specific error message.
// Get current quarter by date
// For example Q1, Q2, Q3, Q4
// moovIT GmbH - Bernhard Dimmel
// Changelog: Jan 7th 2025
// Version 1.0
// Initial commit
// Define a function to get the quarter based on the current month
function getCurrentQuarter(currentMonth) {
if (currentMonth >= 1 && currentMonth <= 3) {
return "Q1";
} else if (currentMonth >= 4 && currentMonth <= 6) {
return "Q2";
} else if (currentMonth >= 7 && currentMonth <= 9) {
return "Q3";
} else if (currentMonth >= 10 && currentMonth <= 12) {
return "Q4";
} else {
return "Invalid Month";
}
}
var currentMonth = {date.month}; // Replace with the current month
// Get the current quarter
var currentQuarter = getCurrentQuarter(currentMonth);
// Display the result
if (currentQuarter !== "Invalid Month") {
currentQuarter;
} else {
currentQuarter = "Invalid input: Please provide a valid month (1-12), detected input: " + currentMonth;
currentQuarter;
}Last updated