cancel
Showing results for 
Search instead for 
Did you mean: 

change a string to a date format

12-17-2019 12:24 AM
edbravo Participant
3715 views 14 comments Go to solution
0 Likes
SAP Managed Tags
Subscribe

Good day to all

First of all, an apology, use Google translator to post my problem.

I would like to put the following column text in a date format “dd / mm / yyyy”.

The data given to me through an Odata created for the use of the app is a String, so I don't know if that is a problem to be able to format it.

I have researched a little, but I have not been able to find something that can help me or work correctly.

This is part of my code:

XML View:

<Table inset="false" items="{/d/results}" id="table0" width="auto">
<items>
 <ColumnListItem type="Active" id="item1">
  <cells>
   <Text text="{Xao}" id="text7"/>
   <Text text="{Xoccupant}" id="text8"/>
   <Text text="{Validfrom}" id="text9"/>
   <Text text="{Validto}" id="text10"/>
  </cells>
 </ColumnListItem>
</items>
<columns>
 <Column id="column0" >
  <header>
   <Label text="Tipo" id="label0"/>
  </header>
</Column>
<Column id="column1">
 <header>
  <Label text="Ocupante" id="label1"/>
 </header>
</Column>
<Column id="column2">
 <header>
  <Label text="Fecha de Apartado" id="label2"/>
 </header>
 </Column>
  <Column id="column3">
   <header>
    <Label text="Fecha de Finalización" id="label3"/>
   </header>
  </Column>
 </columns>
</Table>

Controller.js:

var serviceURL4 = "/sap/opu/odata/sap/ZGESRE_SRV/SCheaderSet?$filter= (AotypeAo eq 'US' and Nivel2 eq '" + this._ciudad + "' and Nivel3 eq '" + this._Edificio + "' and Nivel4 eq '" + this._Piso + "')";
var JsonModel4 = new sap.ui.model.json.JSONModel();
this.getView().byId("table0").setModel(JsonModel4);
JsonModel4.loadData(serviceURL4, null, false);
JsonModel4.getProperty("/d/results");

Any ideas or any advice on how I could modify or change the format to put the date?

Thank you for your answers.

Greetings.

Edson.

0 Likes

Accepted Solutions (1)

Accepted Solutions (1)

s_kancherla
Participant
0 Likes

hi,

since you are using binding, you can mention the type of the bining parameter and provide format options.

Please note that the source->pattern, is the Date pattern in ABAP

and the "pattern" property is for the output pattern for the display.

You can use something like this:

<Text text="{ 
    path: 'Validfrom', 
    type: 'sap.ui.model.type.Date', 
    formatOptions: { 
        source: { 
            pattern: 'yyyyMMdd'
        },
        pattern: 'dd/MM/yyyy'
   }
}"/>
edbravo
Participant
0 Likes

Hi Sidhir, thanks for your answer

do what you told me, but in the Chrome console, I throw the following error.

Did I make any wrong method or does it have to do with the fact that the data obtained is not Date, if not, a string?

thanks for your answer.

Regards.

Edson

Answers (3)

Answers (3)

manju537449
Participant

Hi Edson,

You can write below code and format the date your getting from backend.

var dateFormat = sap.ui.core.format.DateFormat.getDateInstance({pattern:"dd/MM/YYYY" });

var format = dateFormat.parse("20191203");

var dateFormatted = dateFormat.format(format);

Regards,

Manjunatha Devadiga

s_kancherla
Participant
0 Likes

hi Edson,

Please include the following either one of the following properties to in the index.html file

data-sap-ui-compactVersion="edge"

or

data-sap-ui-bindingSyntax="complex"

Ref: https://openui5.hana.ondemand.com/#/topic/91f0652b6f4d1014b6dd926db0e91070

I hope it will solve the error.

with regards

Sudhir

edbravo
Participant
0 Likes

Hi Sudhir.

thank you very much for your help.

The change of the date format was successfully achieved, it was the last thing I needed for this table.

Thanks again for your help.

Regards.

Edson.

s_kancherla
Participant
0 Likes

hi,

happy to hear that it helped.

If the answer has helped please don't forget to accept the helpful answer. So that it helps the others solve a similar issue.

Thank you.

Sudhir

former_member540067
Active Participant
0 Likes

You should use formatter for this thing.

Please refer to the following blogpost for the same.

https://sapyard.com/sapui5-list-control-using-formatters/

and for the formatter function, you can use the following code:

	formatDate: function (oDate) {
		if (oDate) {


			var date = oDate.substring(4, 6) + "/" + oDate.substring(6, 8) + "/" + oDate.substring(0, 4);
return date;
		}
		return "";
	},
edbravo
Participant
0 Likes

Hi Anmol.

Thank you very much for your response and reference of the formatting issue.

I must say that it worked, only that it fits me in the form of MM / DD / YYYY, and I have tried to modify it so that DD / MM / YYYY is changed by changing the data within substring, but it still does not work.

var date = oDate.substring(6, 😎 + "/" + oDate.substring(4, 6) + "/" + oDate.substring(0, 4);

Will you have any advice or any way to spend the months a day?

Regards.

Edson.

edbravo
Participant
0 Likes

Hi Anmol.

Thank you very much for your response and reference of the formatting issue.

I must say that it worked, only that it fits me in the form of MM / DD / YYYY, and I have tried to modify it so that DD / MM / YYYY is changed by changing the data within substring, but it still does not work.

var date = oDate.substring(6, 😎 + "/" + oDate.substring(4, 6) + "/" + oDate.substring(0, 4);

Will you have any advice or any way to spend the months a day?

Regards.

Edson.

former_member540067
Active Participant
0 Likes

Hi Edson

It's strange that var date = oDate.substring(6, 😎 + "/" + oDate.substring(4, 6) + "/" + oDate.substring(0, 4); Isn'tworking.

Can you please use console to check the date before and after the formatting using console.log(oDate) and console.log(date) and reply me with the output?

Regards

Anmol

former_member540067
Active Participant



Edson it is working fine for me.

Regards

Anmol

edbravo
Participant
0 Likes

Hi Anmol.

I could correct the data and I am already with the format that I needed.

thanks for your help.

Regards.