简体   繁体   English

如何使用自定义日期格式 OData V4 Edm.DateTime 值

[英]How to use custom date format an OData V4 Edm.DateTime value

I am struggling to format an OData V4 Edm.DateTime value.我正在努力格式化 OData V4 Edm.DateTime 值。 When I declare it as normal datetime value当我将其声明为正常的日期时间值时

<Label text="{ams>Major}.{ams>Minor}.{ams>Build} (
        { 
          path: 'ams>CreationDate', 
          type: 'sap.ui.model.type.Date', 
          formatOptions: {
            pattern: 'yyyy/MM/dd'
          }
        })"/>

I get following error:我收到以下错误:

TypeError: j.getTime is not a function

If I declare it using the OData data type, nothing is formatted.如果我使用 OData 数据类型声明它,则不会格式化任何内容。

<Label text="{ams>Major}.{ams>Minor}.{ams>Build} (
        { 
          path: 'ams>CreationDate', 
          type: 'sap.ui.model.odata.type.Date', 
          formatOptions: {
            pattern: 'yyyy/MM/dd'
          }
        })"/>

Puts out eg:推出例如:

2016-11-21T17:13:56.207+01:00

Is there any possibility to format it directly in the XML template, or do I have to use a custom formatter?是否有可能直接在 XML 模板中对其进行格式化,还是必须使用自定义格式化程序?

I would use a custom formatter.我会使用自定义格式化程序。 They were created for this kind of task, and if you have multiple dates, you can re-use the same formatter (which is cool).它们是为此类任务创建的,如果您有多个日期,则可以重复使用相同的格式化程序(这很酷)。

Improved Answer:改进答案:

As pointed out by the SAP support this is the way to go when using OData V4:正如 SAP 支持所指出的,这是使用 OData V4 时要走的路:

<Text text="{
              path: 'agent>/CurrentVersion/CreationDate',
              type: 'sap.ui.model.odata.type.DateTimeOffset',
              constraints: {
                precision: 3,
                v4: true
              },
              formatOptions: {
                pattern: 'dd. MM. yyyy'
              }
            }"
            tooltip="{
              path: 'agent>/CurrentVersion/CreationDate',
              type: 'sap.ui.model.odata.type.DateTimeOffset',
              constraints: {
                precision: 3,
                v4: true
              },
              formatOptions: {
                pattern: 'dd. MM. yyyy - hh:mm:ss'
              }
            }"/>

The important part is to give the precision.重要的部分是给出精度。 My OData Service (ASP.NET WEB API) returns datetimeoffset with milliseconds.我的 OData 服务 (ASP.NET WEB API) 以毫秒返回 datetimeoffset。 Therefore, the precision has to be set to 3.因此,精度必须设置为 3。

Original Answer:原答案:

In addition, as I had some problems, here is my approach with a custom formatter:此外,由于我遇到了一些问题,这是我使用自定义格式化程序的方法:

The reason, why you cannot use the common Date format is, that the odata value comes as a string.不能使用通用日期格式的原因是 odata 值作为字符串出现。 I used a custom formatter on my controller and the dateformat.js scripts.我在控制器和dateformat.js脚本上使用了自定义格式化程序。

<Label text="{parts: [{path: 'mymodel>CreationDate', type: 'sap.ui.model.odata.type.Date'},
                        {path: 'i18n>global.dateformat', type: 'sap.ui.model.type.String'}],
                formatter: '.odatadateformatter'}"/>

Here is my formatter code (typescript):这是我的格式化程序代码(打字稿):

var dateFormat: DateFormatStatic;
...
odatadateformatter(datetime: sap.ui.model.odata.type.Date, format?: string): string {
            if(!format)
                format = "yy-mm-dd:hh:MM:ss";
            return dateFormat(new Date(datetime), format);
        }

Any other approaches lead to errors.任何其他方法都会导致错误。 You can now adjust your date accordingly to your i18n information, so you can use different formats for different countries.您现在可以根据您的 i18n 信息相应地调整您的日期,因此您可以为不同的国家/地区使用不同的格式。 I think there is also an option to put in a locale in the dateFormat(...) function我认为还有一个选项可以在 dateFormat(...) 函数中放入语言环境

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM