Skip to Content

SAC Application Designer - Convert Date DD/MM/YYYY to internal format

Hello Experts,

In SAC Application Designer, I have a requirement to get the user input date(DD/MM/YYYY) and internally compute DD/MM/YYYY -2 days( Given Date - 2) in script , this date will be passed to the CDS query as input parameter. As of now I want to perform this calc in frontend and not in CDS level.

The problem I am facing is to convert DD/MM/YYYY to internal Java Script Date format,

I have tried the below 2 options, but it doesn't work

1. var day_ext = Input_Date.getValue(); // day_ext will have the date in DD/MM/YYYY

var mydate = new Date(day_ext);

console.log(mydate.toDateString()); // Error message - String cannot be converted to Integer

So I tried to use the below function to convert string to integer and then used the integer output to get internal date format, but it doesn't work aswell.

2. var datem2 = day_ext.substring(6, 10)+day_ext.substring(3, 5)+day_ext.substring(0, 2); //yyyymmdd, tried all other format var Dateint = ConvertUtils.stringToInteger(datem2);

console.log(Dateint); // The format of Dateint is yyyymmdd in integer

Can you please let me know how can we convert DD/MM/YYYY to integer value equivalent to date, which can be used to get the SAC date format.

Regards,

B

Add a comment
10|10000 characters needed characters exceeded

Related questions

4 Answers

  • Posted on May 05, 2020 at 07:09 AM

    Dear Balaji

    Try something as below

    convert your input to string. then substring it to a date variable. Then use date calculation

    var str = "2020.05.05";  <== your input converted to string
    var year = ConvertUtils.stringToInteger(str.substr(0,4));
    var month = ConvertUtils.stringToInteger(str.substr(5,2));
    var day = ConvertUtils.stringToInteger(str.substr(7,2));
    var date = DateFormat.format(new Date(year,month,day),"MM/dd/yyyy");
    

    Then use the date function to subtract 2

    Regards,

    Venkat

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on May 05, 2020 at 03:50 PM

    All the above code is correct only difference is month start from zero.

    0 = jan

    1= feb

    2 = march

    3 = april

    4 = may

    ETC.

    Please refer the below code.

    var day_ext = Input_Date.getValue();//Datein DD/MM/YYYY
    console.log(day_ext);// My inputdate example :03/04/2020
    var year = ConvertUtils.stringToInteger(day_ext.substring(6,10)); 
    var month = ConvertUtils.stringToInteger(day_ext.substring(3,5)); 
    var day = ConvertUtils.stringToInteger(day_ext.substring(0,2)); 
    var date_s = DateFormat.format(newDate(year,month-1,day),"MM/dd/yyyy"); 
    console.log(date_s);
    var daten=newDate(date_s);//Issue-2 Syntaxerror -Cannot convertstringto Integer
    daten.setDate(daten.getDate()-2);//Not sure if this will workfor MM/dd/yyyy format
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on May 05, 2020 at 12:08 PM

    Hello Venkat,

    Thanks for your reply, much appreciated.

    I tried your logic and still face the same two issues

    var day_ext = Input_Date.getValue(); //Date in DD/MM/YYYY
    console.log(day_ext);  // My input date example : 03/04/2020
    var year = ConvertUtils.stringToInteger(day_ext.substring(6,10)); 
    var month = ConvertUtils.stringToInteger(day_ext.substring(3,5)); 
    var day = ConvertUtils.stringToInteger(day_ext.substring(0,2)); 
    var date_s = DateFormat.format(new Date(year,month,day),"MM/dd/yyyy"); 
    console.log(date_s);// Issue-1 After conversion its 05/03/2020 instead of 04/03/2020
    
    //Given Date -2
    var daten= new Date(date_s); //Issue-2 Syntaxerror -Cannot convert string to Integer
    daten.setDate(daten.getDate()-2); //Not sure if this will work for MM/dd/yyyy format
    
    

    1. The date after conversion to new format is wrong, please check the comments in console statement.

    2. Can you let me know what date function you refer to subtract 2 days from MM/dd/yyyy?

    Regards,

    B

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on May 06, 2020 at 03:10 AM

    Hi,

    Thanks for your reply, much appreciated.

    It seems there is no direct way to calculate offset of days from user input format or after converting to date MM/dd/yyyy.

    Many of the javascript or design studio functions are unfortunately not available/supported in SAC application designer at the moment.

    So I had to do the logic at CDS level which solves the issue.

    Regards,

    B

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.