on 05-05-2020 1:22 AM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
85 | |
10 | |
10 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.