cancel
Showing results for 
Search instead for 
Did you mean: 

Issues with sap.ui.core.format.DateFormat

0 Kudos

Hello UI5 experts,

I'm a new developer and ran into some problems trying to format a JSON Date object and XDS Duration.

I have an application with an XML view, I call the formatter like so:


<Input id="x" value="{path:'item>/Erzet',formatter:'getDateFormat'}"</Input>

My formatter function looks like so:


function getDateFormat(jsonDate){

if(jsonDate){

var value= JSON.stringify(jsonDate);

value1 = value.substring(7,20);

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

var cdate = oDateFormat.format(new Date(Number(value1)));

return cdate;

}return;

}

The above works, but when I use the code I found in UI5's documentation I either get NaN/NaN/0NaN or an error "Uncaught TypeError: d.getDay is not a function". Is there anyway I can format date/time without slicing it? I want to use the .parse method while updating oData.

Snippets of what I've tried:


var oDate = new Date(jsonDate);

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

return oDateFormat.format(oDate);


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

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

^ returns - NaN/NaN/0NaN


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

return oDateFormat.format(jsonDate);

^ returns  "Uncaught TypeError: d.getDay is not a function"

Could someone help me find out what I'm doing wrong here?

Thank you.

Accepted Solutions (1)

Accepted Solutions (1)

karthikarjun
Active Contributor
0 Kudos

Hi AbdulSalam,

Will this helpful? JS Bin - Collaborative JavaScript Debugging

Thanks,

KA

0 Kudos

Hi Karthik, unfortunately that too returns "0NaN/NaN/NaN". I believe I did try too, converting Date to Number after seeing it in another post. It's strange, this should be working but doesn't, I'm sure I've missed something small or obvious somewhere.

Thank you.

karthikarjun
Active Contributor
0 Kudos

Could you please tell me, what was the date format you get it from service?

I mean Erzet this field value

Thanks,

KA

0 Kudos

Hi,

My date comes like so : /Date(1445817600000)/

karthikarjun
Active Contributor
0 Kudos
0 Kudos

Hi Kathik,

My apologies, I missed these statements in your original example.


     var jsonDate = oEvent.Date;

    jsonDate = jsonDate.match(/\d/g);

    jsonDate = jsonDate.join("");

I believe I am doing the same in these statements:

  1. var value= JSON.stringify(jsonDate); 
  2. value1 = value.substring(7,20); 

Is formatting date possible without this conversion?

I viewed this blog - Working with oData Dates and Times and such a conversions wasn't done here. The .parse method of the DateFormat control does the opposite of what we're doing here and I would like to simply use .format when displaying from oData and .parse when writing to oData, this is what was done in the blog I linked but I'm unable to recreate it.

Thank you.

karthikarjun
Active Contributor
0 Kudos

The problem I seen here is substring(7,20) it gives you 1000073882/ ends with /. This is the reason you are getting nan/nan. So better use regular expression to take time stamp.

0 Kudos

Agreed, but I wanted to know if the date be formatted without doing this type of conversion? I use'd substring, you used match and join to achieve the same result albeit my code was slightly wrong.

Thank you for all your help.

karthikarjun
Active Contributor
0 Kudos

If you do not want to do type conversion in formatter.

Make sure your service date field should return your desire date format. If not, you should do either one of this conversion method.

Thanks,

KA

0 Kudos

Thank you for clearing this for me. I appreciate it.

I have a follow up question about Time format.

How do I convert XDS duration PT04H32M00S to EDM time (total milliseconds)? It seems only EDM (total ms) time can formatted with the DateFormat control (according to the blog I linked).

Thank you.

karthikarjun
Active Contributor
0 Kudos

Appreciate your question.

This blog will hep you: EDM.Time is not converting in SAPUI5 | SCN

Thanks,

KA

Answers (0)