SAP UI5日期字段的显示逻辑和用法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/i042416/article/details/88072628

Sent: Tuesday, December 23, 2014 2:27 PM

这里是Opportunity Notes UI上Date Format的逻辑:

<FeedListItem sender="{path : 'json>Creator'}"  senderActive="false"
timestamp="{path:'json>CreatedAt' , formatter: 'cus.crm.opportunity.util.Formatter.notesDateFormatter'}"
text="{path : 'json>Content'}"
/>

每一条Notes Item上面都可以绑定一个Date Formatter,对应的是Formatter.js文件下的notesDateFormatter方法:

           notesDateFormatter: function (oValue) {
if(oValue === "" || oValue === null || oValue === undefined)
      return "";
if(!(oValue instanceof Date)){
// 转换成javascript标准的Date对象
                         oValue = new Date(oValue);
                     }
 
// 下面这行将当前日期和GMT标准时间的时间差算出来,然后换算成GMT标准时间,然后再用GMT的时间做Format.
                     // 这个做法感觉有点别扭,只是为了把日期format成GMT的string,但是已经把oValue这个日期对象的值改变了,如果接下来还要用oValue做别的事情或者显示的日期时间需
// 要根据configuration里面的locale来显示就会有问题。
                     oValue.setMinutes(oValue.getTimezoneOffset());
var locale = new sap.ui.core.Locale(sap.ca.scfld.md.app.Application.getImpl().getResourceBundle().sLocale);
// 用DateFormatter来Format日期  
var formatter = sap.ca.ui.model.format.DateFormat.getDateInstance({style : "medium"},locale);
return formatter.format(oValue);
                },

处理GMT时间差之前的时间:

clipboard1

处理GMT时间差之后的时间:

clipboard2

可以看到时区没变,但是从小时上减去了8个小时,相当于日期对象的值已经变了。
关于Note的时间要支持到时分秒,有两种办法:
UI5除了Date Format, 还提供了DateTime Format:

clipboard3

给Date Format传入时分秒的pattern:

clipboard4

这里就可以看出来,如果要支持显示时区的话,直接用原来的代码就有问题,时间应该是+8区的14点,而不是+8区的6点。
具体的日期Format的逻辑可以看sap.ui.core.format.DateFormat.js里面的code。

有时间的时候看一下前台怎么显示note creation date的吧:

QHD/504, Opp id 5576

后台返回5个note,已经按照timestamp排好序了。

clipboard5

5条都是今天创建的,
为什么我最早创建的反而是2014-12-22, 而最新的4条显示的是2014-12-21创建的?

clipboard6

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

猜你喜欢

转载自blog.csdn.net/i042416/article/details/88072628