cancel
Showing results for 
Search instead for 
Did you mean: 

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

Accepted Solutions (0)

Answers (4)

Answers (4)

saurabh_sonawane
Active Contributor

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
mukesh_patwa
Explorer
0 Kudos

Thanks Saurabh, It worked for me.

saurabh_sonawane
Active Contributor
0 Kudos

Hi Mukesh,

Welcome.

you can click the upvote (like ) correct answer and it help other people too.

Thanks,

Saurabh S.

venkateswaran_k
Active Contributor

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

0 Kudos

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

saurabh_sonawane
Active Contributor
0 Kudos

Great

Can you please close the question.

Thanks & Regards,

Saurabh S.

0 Kudos

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