cancel
Showing results for 
Search instead for 
Did you mean: 

Convert Date Field in an Object Array of XSJS

Former Member
0 Kudos

Dear All,

I am using below code in my xsjs to read data from Hana View. In the ResultSet Object,

a) In records Resultset object, I have all date fields coming in with value as 2017-10-12T00:00:00:000Z.

b) And few garbage values.

How can I modify the date object to represent just dd-mm-yyyy and get rid of absurd entries please ?

XSJS Code currently :

var conn = $.hdb.getConnection();
str = "SELECT *  FROM  WHERE \"COMPANY_CD\"=? AND \"DOC_KEY\" =? AND \"SYSTEM_ID\"=? AND \"POSTING_DATE\" BETWEEN ? AND ?"; 
records = conn.executeQuery(str, company, key, applnid, startdate, enddate);

I tried doing the below in a loop to select Date field in records Object and manipulating it. But it gave me a Type Error that the Date field is Read-Only.

records[i].INVOICE_DATE = (records[i].INVOICE_DATE.toString()).split('T')[0];

Can you please help on how can I modify records JSON with right values (and also by avoiding garage values like (-000001-12-31T00:00:00.000Z) please ?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Hari,

I create a function to get your right format.

function formatDate (input)
	{
	var d,m,y;
	d= input.getDate();
	if(Number(d)<10){
		d= '0'+d;}
	m = input.getMonth();
	m= Number(m)+1;
	if(Number(m)<10){
		m= '0'+m;}
	y= input.getFullYear();
	
	return (d+'.'+m+'.'+y);

       }

INVOICE_DATE = formatDate(records[i].INVOICE_DATE)

I hope that it will help you.

Best regards

Houssem

Former Member
0 Kudos

Hi Houssem,

Thank you for quick reply. "records" is an object array. It has several keys and Date is one among them.

So when I am trying to modify Invoice_Date with the new value and modifying it back in the array, it is giving me Type Error that "Invoice_Date" is Ready Only I am afraid.

Thanks

Former Member
0 Kudos

Yes you should create a new variable to store the new data.

Here an example.

Var NewArray = [];
for ( var i = 0 ;i<records.length ;i++)
			{
			entry= {}
			entry.X = records[i]["X"]
			entry.Y = formatDate(records[i]["Y"])
			entry.Z = formatDate(records[i]["Z"])
			entry.A=records[i]["A"]
			entry.B =records[i]["B"]	
			output.NewArray.push(entry);
			}

Former Member
0 Kudos

Thank you Houssem. Looping again across columns was causing a bit of overhead on xsjs execution. So we pushed down the logic to Hana DB to modify Dates to Strings on the Hana View level.

Answers (0)