Author Topic: Convert Date Format  (Read 2688 times)

Benjamin Dasari

  • GuiXT Forum
  • Newbie
  • *
  • Posts: 95
    • View Profile
Convert Date Format
« on: August 03, 2016, 02:47:18 PM »
Purpose:
Convert user entered date format to a new format.

Liquid UI Code:

// SAPLSMTR_NAVIGATION.E0100.sjs

// Function to trim blank spaces at the end of the string
String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,'');}
                     
// Function to check if the string value is blank
function isBlank(jvar){
   if(typeof jvar == 'string') {
      jvar = jvar.trim();
   }
   if(typeof jvar == 'undefined') {
      jvar = '';
   }
   return(jvar == 'undefined' || jvar == undefined || jvar == null || jvar == "" || jvar == void 0);
}

/**************************************************************************************
Change date from one format to another formats:
   // 1. DD.MM.YYYY
   // 2. MM/DD/YYYY
   // 3. MM-DD-YYYY
   // 4. YYYY.MM.DD
   // 5. YYYY/MM/DD
   // 6. YYYY-MM-DD
****************************************************************************************/

function changeFormat(date,dformat,outformat){
    arDateValues = check_date_format(date,dformat);
   if (arDateValues.length == 0){
      return ("E: Invalid Format");
   }
   day = arDateValues[0];
   month = arDateValues[1];
   year = arDateValues[2];
   date1 = "";
   switch (outformat){
         case '1':
            date1 = day + "." + month + "." + year;
            break;
         case '2':
            date1 = month + "/" + day + "/" + year;         
            break;
         case '3':
            date1 = month + "-" + day + "-" + year;                     
            break;
         case '4':
            date1 = year + "." + month + "." + day;         
            break;
         case '5':
            date1 = year + "/" + month + "/" + day;            
            break;
         case '6':
            date1 = year + "-" + month + "-" + day;
            break;
      }
      return (date1);   
}

// Function to validate the date format
function check_date_format(date,dformat){
   date = date.toString();
   arValues = [];
   arTmpValues = [];
   switch (parseInt(dformat)){
      case 1:
         var matches = /^([0]?[1-9]|[12][0-9]|3[01])\.([0]?[1-9]|1[012])\.[0-9]{4}/.exec(date);
         if (matches == null) return ([]);
         else {
            arTmpValues = date.split(".");
            return (arTmpValues);
         }
         break;
      case 2:
         var matches = /^([0]?[1-9]|1[012])\/([0]?[1-9]|[12][0-9]|3[01])\/[0-9]{4}/.exec(date);
         if (matches == null) return ([]);
         else {            
            arTmpValues = date.split("/");
            return ([arTmpValues[1],arTmpValues[0],arTmpValues[2]]);
         }
         break;
      case 3:
         var matches = /^([0]?[1-9]|1[012])\-([0]?[1-9]|[12][0-9]|3[01])\-[0-9]{4}/.exec(date);
         if (matches == null) return ([]);
         else {
            arTmpValues = date.split("-");
            return ([arTmpValues[1],arTmpValues[0],arTmpValues[2]]);
         }
         break;   
      case 4:
         var matches = /^[0-9]{4}\.([0]?[1-9]|1[012])\.([0]?[1-9]|[12][0-9]|3[01])/.exec(date);
         if (matches == null) return ([]);
         else {
            arTmpValues = date.split(".");
            return ([arTmpValues[2],arTmpValues[1],arTmpValues[0]]);
         }
         break;
      case 5:
         var matches = /^[0-9]{4}\/([0]?[1-9]|1[012])\/([0]?[1-9]|[12][0-9]|3[01])/.exec(date);
         if (matches == null) return ([]);
         else {
            arTmpValues = date.split("/");
            return ([arTmpValues[2],arTmpValues[1],arTmpValues[0]]);
         }
         break;
      case 6:
         var matches = /^[0-9]{4}\-([0]?[1-9]|1[012])\-([0]?[1-9]|[12][0-9]|3[01])/.exec(date);
         if (matches == null) return ([]);
         else {
            arTmpValues = date.split("-");
            return ([arTmpValues[2],arTmpValues[1],arTmpValues[0]]);
         }
         break;
   }
}

// Function to call the check and change date format functions
function changeDateFormat(){
   if(isBlank(z_date)){
      message('E: Please enter Date');
      goto SCRIPT_END;
   }
   if(isBlank(z_currdtformat)){
      message('E: Please enter Current Format');
      goto SCRIPT_END;
   }
   if(isBlank(z_newdtformat)){
      message('E: Please enter New Format');
      goto SCRIPT_END;
   }
   
   z_nwdtformat = changeFormat(z_date,z_currdtformat,z_newdtformat);
   if(z_nwdtformat.indexOf('Invalid') > -1){
      message('E: Invalid format - Date and Current Format should match');
      goto SCRIPT_END;
   } else{
      set('V[z_date]','&V[z_nwdtformat]');
      message('S: Changed format successfully');
   }
   enter('?');
   
   SCRIPT_END:;
}

// User Interface
clearscreen();
inputfield([1,1], "Enter Date", [1,22], {"name":"z_date", "required":true, "size":10});

text([2,1], "Select Current Format");
set("V[Current Format]", "=;1=1 DD.MM.YYYY;2=2 MM/DD/YYYY;3=3 MM-DD-YYYY;4=4 YYYY.MM.DD;5=5 YYYY/MM/DD;6=6 YYYY-MM-DD;");
dropdownlist([2,22], "Current Format", {"refer":"z_currdtformat", "width":15, "textfont":"verdana", "textheight":13, "textweight":4});

text([3,1], "Select New Format");
set("V[New Format]", "=;1=1 DD.MM.YYYY;2=2 MM/DD/YYYY;3=3 MM-DD-YYYY;4=4 YYYY.MM.DD;5=5 YYYY/MM/DD;6=6 YYYY-MM-DD;");
dropdownlist([3,22], "New Format", {"refer":"z_newdtformat", "width":15, "textfont":"verdana", "textheight":13, "textweight":4});

pushbutton([5,1], "@01@Change Date to New Format", "?", {"process":changeDateFormat});


See attachments for code samples!
« Last Edit: September 21, 2016, 03:43:53 PM by Benjamin Dasari »