Liquid UI - Documentation - 9.04 Get current date in a specific format in SAP

9.04 Get current date in a specific format in SAP


Prerequisites


Purpose

Learn how to get today's date in user's date format with a pushbutton created on an SAP screen. We will walk you through the following steps.

  1. Delete unnecessary screen elements
  2. Create an input field to display the current date
  3. Add a pushbutton to call the function that displays the current date
  4. Create a function to remove blank spaces from variable values
  5. Create a function to get today’s date in the user’s date format
  6. Create a function to pad a string with characters


User Interface


Create this file inside your script folder for customizing SAP Easy Access screen SAPLSMTR_NAVIGATION.E0100.sjs
//Now, let's start adding the following script content to the above file.


Customization
  1. Open SAP Easy Access screen and delete the image container on it, as shown below:
     
    // Deletes an image container on SAP Easy Access screen 
    del("X[IMAGE_CONTAINER]");
    
     
  2. Add an input field to display the current date.
     
    // Creates an input field with the label as Date to display the date. 
    inputfield( [2,4], "Date", [2,16],{"name":"z_todaydate", "size":10});
    
     
  3. Add a pushbutton to call the function, when clicked.
     
    // Creates a pushbutton with the label as Get Today's Date to call a function, when clicked. 
    pushbutton([4,8], "Get Today's Date","/nsu3",{"size":[2,22], "process":today_date});
     
     
  4. Now, add the following Liquid UI script to this file, and save the file.
     
    function today_date(){ 
    Maintain User Profile
    onscreen 'SAPLSUID_MAINTENANCE.1100'
    enter('=DEFA');

    // Maintain User Profile
    onscreen 'SAPLSUID_MAINTENANCE.1100'
    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 today's date with user 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;
    }

SAP Process
  1. Now, refresh the SAP screen, and click the Get Today's Date pushbutton. Then, you will get today’s date displayed on the input field Date, as shown in the image below.
     
     

Can't find the answers you're looking for?