UI5应用程序中的日期不匹配

时间:2017-09-03 14:06:41

标签: sapui5

我在UI5应用程序中有一个DatePicker。我的服务器在澳大利亚。当我在IST时间创建任何记录时,它工作正常。但是当用户尝试在澳大利亚创建任何记录时,日期值会增加1.即"31""32"。我需要考虑时区吗?

2 个答案:

答案 0 :(得分:2)

如果您在sap.ui.model.odata.type.Date*value绑定定义中使用绑定类型DatePicker,则UI5已经正确处理日期。

  • 通过将UTC: true添加到formatOptions来显示 UTC中的日期。
  • 通过将isDateOnly : true添加到constraints,将日期保存为UTC。

      

    如果为true,则仅使用日期部分,时间部分始终为00:00:00,时区为UTC 以避免与时区相关的问题



sap.ui.getCore().attachInit(() => sap.ui.require([
  "sap/ui/model/odata/v2/ODataModel",
  "sap/ui/core/mvc/XMLView",
  "sap/ui/thirdparty/datajs"
], (ODataModel, XMLView) => XMLView.create({
  definition: `<mvc:View
    xmlns:mvc="sap.ui.core.mvc"
    xmlns="sap.m"
  >
    <DatePicker id="myDatePicker"
      busy="true"
      binding="{
        path: '/Employees(1)',
        parameters: {
          select: 'EmployeeID, HireDate'
        }
      }"
      value="{
        path: 'HireDate',
        type: 'sap.ui.model.odata.type.DateTime',
        constraints: {
          isDateOnly: true,
          displayFormat: 'Date'
        }
      }"
      width="12rem"
    />
  </mvc:View>`,
  models: new ODataModel({
    serviceUrl: "https://cors-anywhere.herokuapp.com/https://services.odata.org/V2/Northwind/Northwind.svc/",
    defaultBindingMode: "TwoWay",
    preliminaryContext: true,
    tokenHandling: false,
  }),
}).then(view => {
  const messageManager = sap.ui.getCore().getMessageManager();
  messageManager.registerObject(view.placeAt("content"), true);
  const dp = view.byId("myDatePicker");
  dp.getElementBinding().attachDataReceived(() => dp.setBusy(false));
})));
&#13;
<script src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js" id="sap-ui-bootstrap"
  data-sap-ui-libs="sap.ui.core, sap.m, sap.ui.unified"
  data-sap-ui-theme="sap_belize"
  data-sap-ui-preload="async"
  data-sap-ui-compatVersion="edge"
  data-sap-ui-xx-waitForTheme="true"
  data-sap-ui-xx-async="true"
  data-sap-ui-xx-xml-processing="sequential"
></script><body id="content" class="sapUiBody sapUiSizeCompact"></body>
&#13;
&#13;
&#13;

  1. 绑定类型解析本机JS日期对象中的传入Emd.DateTime字符串,并将其存储在模型中,只要约束没有违反
  2. datajs,在formats the date内部使用,{{3}}在发送到后端之前正确使用。

答案 1 :(得分:0)

   prepareDatesToDisplay: function(oDate){ //to display dates from backend
                var oTempDate = new Date(oDate);
                oDate = new Date(oTempDate.getTime() + oTempDate.getTimezoneOffset() * (60000));
                return oDate;
    },
    changeDateToUTC: function(oDate){ //for sending dates to backend
                var oTempDate = new Date(oDate.setHours("00","00","00","00"));
                oDate = new Date(oTempDate.getTime() + oTempDate.getTimezoneOffset() * (-60000));
                return oDate;
    },