Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current Month Google Apps Script (a lot of IF-statements)

I have done a script that prints out the current month in the Logger. I think this could be done in a better way with Arrays.

This is what I have so far:

function myFunction() {

var date = new Date();
var mt = date.getMonth();
var currentD     

  if  (mt === 0) {
   currentD = "JAN (qty)";
  }if (mt === 1) {
   currentD = "FEB (qty)";
  }if (mt === 2) {
   currentD = "MAR (qty)";
  }if (mt === 3) {
   currentD = "APR (qty)";
  }if (mt === 4) {
   currentD = "MAJ (qty)";
  }if (mt === 5) {
   currentD = "JUNI (qty)";   
  }if (mt === 6) { 
   currentD = "JULY (qty)"; 
  }if (mt === 7) {
   currentD = "AUG (qty)";  
  }if (mt === 8) {
   currentD = "SEP (qty)";  
  }if (mt === 9) {
   currentD = "OKT (qty)";  
  }if (mt === 10){
  currentD = "NOV (qty)";
  }else if (mt === 11){
  currentD = "DEC (qty)";
  }

 Logger.log("Current Month is: " +currentD); 
}
like image 718
John Smith Avatar asked Oct 26 '15 10:10

John Smith


Video Answer


1 Answers

There's a Utility for that.

var currentD = Utilities.formatDate(date, Session.getScriptTimeZone(), "MMM");
like image 198
Mogsdad Avatar answered Oct 28 '22 05:10

Mogsdad