cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Date Formatting

Former Member
0 Likes
6,734

Hi All,

I created a table with XML view.

This is how i have binded.

<items>

<ColumnListItem>

<cells>

          <Text

            text="{empModel>Empno}" />

          <Text

            text="{empModel>Fname}" />

          <Text

            text="{empModel>Lname}" />

          <Text

            text="{empModel>Addrs}" />

          <Text

            text="{empModel>Desgn}" />

          <Text

            text="{empModel>Dob}" />

</cells>

</ColumnListItem>

    </items>



But my date format which is coming from gateway is in this format "2015-05-05T05:13:19.3167890".


I want to display it as 05-05-2015.


I also have a datepicker and in that datepicker date format is not selected in this format. So basically how i can perform the POST operations?


I am a beginner so if possible attach a simple code for reference.


Thanks in advance.

Accepted Solutions (1)

Accepted Solutions (1)

0 Likes

Hi,
you can try 2 options
1. use type and formatOptions
SAPUI5 Explored

2. use formattrer function

SAPUI5 - XML view and Formatter | SCN

Former Member
0 Likes

Hi,

I tried like this.

          <Text text="{

                     path: '{empModel>Dob}',

                     type: 'sap.ui.model.type.DateTime',

                     formatOptions: {

                           style: 'short',

                           source: {

                                         pattern: 'yyyy/MM/dd'

                                         }

                                                }

}" />


But it didn't worked. It's showing me blank column.


May you provide me a simple example.

0 Likes

Hi Raghu,
please, try that
Code in view


<Text text="{path: 'empModel>Dob',

             formatter: '.formatDate'}"

/>

function in controller:


formatDate : function(v) {

     jQuery.sap.require("sap.ui.core.format.DateFormat");

     var oDateFormat = sap.ui.core.format.DateFormat.getDateTimeInstance({pattern: "dd-MM-YYYY"});

     return oDateFormat.format(new Date(v));

}

Regards,
Raman

Qualiture
SAP Mentor
SAP Mentor
0 Likes

Well, you forgot to specify the input pattern Also, you used brackets surrounding the model field you are binding, but this is incorrect too (you are already binding)

Change it to

<Text text="{

    path: 'empModel>Dob',

    type: 'sap.ui.model.type.Date',

    formatOptions: {

        source: {

            pattern: 'yyyy-MM-ddTHH:mm:ss.AAAAAAA'

        },

        pattern: 'yyyy/MM/dd'

    }

}" />

Former Member
0 Likes

Hi    ,

First of all thank you.

I tried your code but i would like to know here you have used something like this.

formatter: '.formatDate'}"

I see a dot here but in controller you are using just formatDate.

Why you have used dot here??

Former Member
0 Likes

Hi 

I tried your code but i am getting following error.

Please suggest.

And Thanks a lot.

Former Member
0 Likes

pattern: 'yyyy-MM-ddTHH:mm:ss.AAAAAAA', I am getting source pattern like this.


here what is the meaning of .AAAAAAA??


Suppose i have a calendar as input then how i will append this value to my formatter?


Thank you.

Qualiture
SAP Mentor
SAP Mentor
0 Likes

You probably did something wrong then... works for me in this working example: Edit fiddle - JSFiddle

Qualiture
SAP Mentor
SAP Mentor
0 Likes

Since your example date was "2015-05-05T05:13:19.3167890", I assumed the last 7 digits were milliseconds.

But obviously, milliseconds can only be maximum of 3 digits (hence the milli) so i'm not sure what those last 7 digits are. If you look at the example I just provided, you'll see a correct date

0 Likes

You are welcome:)
UI5 uses this syntax for binding. Dot means a local function/type.
Read more SAPUI5 SDK - Demo Kit

Former Member
0 Likes

Hi,

I checked your link and it's working.

But in my case it is not working.

What may be the probable error??

Please suggest.

Thanks a lot.

Qualiture
SAP Mentor
SAP Mentor
0 Likes

As I mentioned, the date you provided has some strange 7 digits at the end where normally only 3 are possible. Can you explain what those 7 digits are, and where they come from?

0 Likes

Perhaps, number 19.3167890 means seconds in float format.

Try pattern 'yyyy-MM-ddTHH:mm:ss.sssssss'.

Former Member
0 Likes

It's coming from my Gateway only. Should i contact to gateway developer?

Qualiture
SAP Mentor
SAP Mentor
0 Likes

No, is right, it is seconds in float format.

Source pattern pattern: 'yyyy-MM-ddTHH:mm:ss.sssssss' should work fine

Former Member
0 Likes

WOW Great. Thanks a lot. Now your code is working.

This post contains two correct answers, but i may only choose one as correct.

former_member197827
Participant
0 Likes

Hi Robin,

From OData service I am receiving the date in following format:

/Date(1354665600000)/"

and I am binding it to sap.ui.commons.DatePicker.

So what should be the source pattern in my case?? I dont want to use formatter function as  it is not updating the changed values in model (I am using json model).

Thanks and Regards,

Sagar

Answers (5)

Answers (5)

Former Member
0 Likes

I have used the following code in the controller where I make my table in:


var datetimecol = new sap.ui.table.Column({label: "{i18n>Date_Time}", editable: false,

  template: new sap.ui.commons.TextField( {

  value: {

  path: "DATETIME",

  formatter: sap.ui.demo.table.util.Formatter.date,

  },editable : false,

  }),

  sortProperty: "Date_Time", width: "12em" });

  oTable.addColumn(datetimecol);

This creates a column and adds it to the table.

This is the code I use for my formatting of the date-time:


date : function(fValue){

  if(fValue != null){

       fValue = new Date(parseInt(fValue.substr(6)));

       var sLocale = sap.ui.getCore().getConfiguration().getLanguage();

       var oBundle = jQuery.sap.resources({url : "i18n/messageBundle.properties", locale: sLocale});

       var dateFormat = sap.ui.core.format.DateFormat.getDateTimeInstance({pattern : oBundle.getText("DateTimeFormat") });

       fValue = dateFormat.format(fValue);

       return fValue;

  }

  return fValue;

   },

I also check the locale so it automatically adjusts to the right time.

The i18n/messageBundle.properties content is:

#DateTime format

DateTimeFormat=dd/MM/yyyy HH:mm:ss

I've done this so that it's possible to change the date time for America or Europe or anywhere else.

I hope this code can be of use to you.

Kind regards,

Max

Former Member
0 Likes

I will try it next time.

Former Member
0 Likes

Thank you 

Solution provided by Raman is working but i am wondering that whu solution of

is not working in my case. Where might be error?

And one more question.

Suppose i am selecting date from calendar then how i will convert this date for Gateway post?

If i will follow the Raman code then in pattern i will define like this?? pattern: "yyyy-MM-ddTHH:mm:ss.AAAAAAA"??


Same code will be applicable for javascript view also?

Private_Member_15166
Active Contributor
0 Likes
Former Member
0 Likes

Dear Experts,

Please help me with this issue.

I will be very grateful for this.

Please consider a model of data value 2015-05-05T05:13:19.3167890.


And please explain how to do this.

Eagerly waiting for you.

NagaPrakashT
Contributor
0 Likes

Hi Raghu,

You can write the below code in your formatter function

  1. formatDate : function(oDate) { 
  2.           var formatDate = new Date(oDate);
  3.           var date = formatDate.getDate();
  4.           var month = formatDate.getMonth() + 1;  // as month starts from 0
  5.           var year   = formatDate.getFullYear();
  6.           var ouput = year + "-" + month + "-" +date;
  7.      return  output;

Thanks,

Naga

Qualiture
SAP Mentor
SAP Mentor
0 Likes

IMO, a formatter is a bit overkill (it requires an extra function in the controller, and more importantly, it is only one-way).

The sap.ui.model.type.Date datatype seems way more versatile

NagaPrakashT
Contributor
0 Likes

Hi Raghu,

Can you please look at existing threads if they are helpful.

Thanks,

Naga

Former Member
0 Likes

I didn't understand that what has been written here.