Author Topic: Get today's date  (Read 4657 times)

rajesh.sabbineni

  • GuiXT Forum
  • Newbie
  • *
  • Posts: 26
    • View Profile
Get today's date
« on: January 28, 2016, 04:18:51 PM »
Get today's date in user's date format


// Code Sample: User Interface

inputfield ([2,4], "Date", [2,16],{"name":"z_todaydate", "size":10});
pushbutton([4,8], "Get Today's Date","/nsu3",{"size":[2,22], "process":today_date});

// Function to get user formatted date

function today_date(){
// Maintain User Profile
onscreen 'SAPLSUU5.0100'
   enter('=DEFA');

// Maintain User Profile
onscreen 'SAPLSUU5.0100'
   set("V[z_dateformat]", "&F[Date format]");
   z_dateformat = z_dateformat.trim();   
   z_todaydate = getTodaysDate(z_dateformat);               
   enter('/n');   

}

////////////////////////////////////////////////////////////////////////////////////////////
//Function prototypes, to remove blank spaces from variable values
////////////////////////////////////////////////////////////////////////////////////////////
String.prototype.trim = function() {
   return this.replace(/^\s+|\s+$/g,"");
}


////////////////////////////////////////////////////////////////////////////////////////////
//Function to get todays date and convert the date to users date format
////////////////////////////////////////////////////////////////////////////////////////////
function getTodaysDate(dformat) {
   var z_date = new Date();
   println(">>>>>>>>>>>>>>>>>",z_date);
   var str = "";
   switch(dformat)
   {
      case '1':
      {
         str = padString(z_date.getDate(),2,PADDING_LEFT,"0") + "." + padString((z_date.getMonth()+1),2,PADDING_LEFT,"0") + "." + z_date.getFullYear();
      }
      break;
      
      case '2':
      {
         str = padString((z_date.getMonth()+1),2,PADDING_LEFT,"0") + "/" + padString(z_date.getDate(),2,PADDING_LEFT,"0") + "/" + z_date.getFullYear();
      }
      break;

      case '3':
      {
         str = padString((z_date.getMonth()+1),2,PADDING_LEFT,"0") + "-" + padString(z_date.getDate(),2,PADDING_LEFT,"0") + "-" + z_date.getFullYear();
      }
      break;

      case '4':
      {
         str = z_date.getFullYear() + "." + padString((z_date.getMonth()+1),2,PADDING_LEFT,"0") + "." + padString(z_date.getDate(),2,PADDING_LEFT,"0");
      }
      break;

      case '5':
      {
         str = z_date.getFullYear() + "/" + padString((z_date.getMonth()+1),2,PADDING_LEFT,"0") + "/" + padString(z_date.getDate(),2,PADDING_LEFT,"0");
      }
      break;
      
      case '6':
      {
         str = z_date.getFullYear() + "-" + padString((z_date.getMonth()+1),2,PADDING_LEFT,"0") + "-" + padString(z_date.getDate(),2,PADDING_LEFT,"0");
      }
      break;
      case '7':
      {
         str = padString(z_date.getDate(),2,PADDING_LEFT,"0") + padString((z_date.getMonth()+1),2,PADDING_LEFT,"0") +z_date.getFullYear();
      }
      break;
   }
   println(str);
   return str;
}

////////////////////////////////////////////////////////////////////////////////////////////
//Function to pad a string with characters
////////////////////////////////////////////////////////////////////////////////////////////
const PADDING_LEFT = 0;
const PADDING_RIGHT = 1;
function padString(source,length,direction,character) {
   var loop;
   var output = "";
   var sourceLength = 0;
   set('V[z_source]',source);
   if(z_source) {
      sourceLength = z_source.length;
   }
   
   switch(direction) {
   case PADDING_LEFT:
      for(loop = 0; loop < (length - sourceLength); loop++) {
         output += character;
      }
      output = output + z_source;
      break;
      
   case PADDING_RIGHT:
      for(loop = 0; loop < (length - sourceLength); loop++) {
         output += character;
      }
      output = z_source + output;
      break;
   }
   return output;
}