cancel
Showing results for 
Search instead for 
Did you mean: 

DateFormat depends by the user profile

0 Kudos
551

Hey Experts!

I have a problem with my date formatter on my View. My date should be formatted according to the user profile. I get my date time from a database by a oDataModel like: Fri Aug 26 2016 02:00:00 GMT+0200 (W. Europe Daylight Time)

and as i mentioned i want to fomat it accoring to the user profile with this code snipped:

// For date/time values require the DateFormat class

jQuery.sap.require('sap.ui.core.format.DateFormat')

// Instantiate the date, time, datetime classes, without a parameter or based on a certain style (either 'short', 'medium' or 'long')

var oDateFormat = sap.ui.core.format.DateFormat.getDateInstance();

// Use any of the variables above to format a date or time value (using JavaScript "Date" objects):

oDateFormat .format(new Date());

The problem is my oData date is a string and not a js-object which i need to format my oData model output correctly.

Have you an idea to fix this problem? fyi i want to implement this datefommater into a ColumListItem control which used as a template to bind my models to a table below this text you will see how i do it at the moment:

Here is how my template looks like with a working but static date time formatter 1# (i want replace this static fomatter with the new method above):

1# My static formatter

2# My template for binding

Best Regards,
Danilo

View Entire Topic
former_member203031
Contributor
0 Kudos

Hi Danilo,

I think the date format you are getting from odata might be in this format

var date = "2016-08-26T00:00:00";

Now you want to convert this date in the regular format like "MM-dd-yyyy"

to achieve this please follow the piece of code :

var frmt2 = sap.ui.core.format.DateFormat.getDateInstance({pattern:"MM-dd-yyyy"})

var new_date = frmt2.format(date)

now you use this new_date to display.

Thanks,

Deepak.

0 Kudos

Hey Deepak,


you are correct with your variant of code it works also with this string:

var sDate= new sap.m.Text({ text: "Fri Aug 26 2016 02:00:00 GMT+0200 (W. Europe Daylight Time)" })

and this is the string which i got from my model.

But now i know its working when i paste a string in my frmt2.format(new Data(sDate)) but i need the dates from my model like i get it before with a path to my oDataModel( without the property: "type" in this case:

var sDate= new sap.m.Text({ text: { path: "project>ENDDATE"}})

But then my output will be a empty string

former_member203031
Contributor
0 Kudos

are you expecting the format of the date should be changed in the model?

and you trying to fetch from model to display the date?

0 Kudos

yes I am expecting  that because our shell takes this control and changes that depends the user profile I also have to implement this control and show the model dates which I fetched like i did it before. The thing what is confusing me is that I can not fetch the model anymore if I do it like the post above.

former_member203031
Contributor
0 Kudos

I am not sure weather it is a correct approach or not you can do like this

for example now you are getting date like this

model.results[0].date = "2016-08-26T00:00:00"

now you want to chane this format of the date

var frmt2 = sap.ui.core.format.DateFormat.getDateInstance({pattern:"MM-dd-yyyy"})

model.results[0].date = frmt2.format(model.results[0].date)


Now the date format in your model will be changed in the required pattern and you can bind it to the view.


Thanks,

Deepak.



0 Kudos

Hey Deepak,

I do not need to prepare my code like that because my tableBinding is doing it:

this.oTable.bindItems("project>ProjectView", oTableTemplate, null, this.oFilter)

that´s why I only need the path to the model>property and the table is doing the rest... mhm

former_member203031
Contributor
0 Kudos

you mean to say that you got the solution and everything is fine?

0 Kudos

Unfortunately not! That is the point why i am so confused. I should get the date like all my other properties which i fetch from the model but if i want to commit my variable with the text string it will be empty. If I write immediately write this textControl in my oTableTemplate the following code  it works fine with the right date:

var sDate= new sap.m.Text({ text: { path: "project>ENDDATE"}})

That means in my point of view that i can fetch a model on my view in a variable like I did it with the sDate

Or I am wrong? Thats why i tried to put the sDate into my frmt2.format(new Data(sDate)) in hope that sDate commit the right string to this function.

former_member203031
Contributor
0 Kudos

could you please tell me how you are binding the remaining elements.

by this i can expect a solution.

former_member203031
Contributor
0 Kudos

Check the following image how i set the date

hope this will get you the required solution.

0 Kudos

I screenshot the whole view with the way I usually use path bindings to my table from a model( this view has not the "new code" implemented to take it clear)

former_member203031
Contributor
0 Kudos

If you are formatting the date in the controller then there is no need to declare the type in the view I mean that date formatting is not required.

there is one method of defining the date please check the screen shot:

Message was edited by: Deepak Anumula

0 Kudos

i realized the methods are the same i had just to remove the pattern...