最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert datetime string to date in SAPUI5? - Stack Overflow

programmeradmin1浏览0评论

I am trying to convert String to Date in SAPUI5

var Fdate = "2020-08-01T00:00:00";
var dateFormat = sap.ui.core.format.DateFormat.getDateInstance({
    pattern: "dd/MM/yyyy"
});
fdateFormatted = dateFormat.format(Fdate);

But this returns the following error:

Uncaught TypeError: j.getTime is not a function

I tried to set the source pattern as "yyyy-MM-ddTHH:mm:ss". But this returns the same error.

I am trying to convert String to Date in SAPUI5

var Fdate = "2020-08-01T00:00:00";
var dateFormat = sap.ui.core.format.DateFormat.getDateInstance({
    pattern: "dd/MM/yyyy"
});
fdateFormatted = dateFormat.format(Fdate);

But this returns the following error:

Uncaught TypeError: j.getTime is not a function

I tried to set the source pattern as "yyyy-MM-ddTHH:mm:ss". But this returns the same error.

Share Improve this question edited Aug 24, 2020 at 15:46 Boghyon Hoffmann 18.1k14 gold badges93 silver badges205 bronze badges asked Aug 10, 2020 at 16:11 d33ad33a 7501 gold badge17 silver badges42 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

In JS

// DateFormat required from "sap/ui/core/format/DateFormat"
const dt = DateFormat.getDateTimeInstance({ pattern: "dd/MM/yyyy" });
const jsDateObject = dt.parse("2020-08-01T00:00:00"); // returns: Sat Aug 01 2020 00:00:00 <timezone information>
const dayMonthYear = dt.format(jsDateObject) // returns: "01/08/2020"
  • If the string contains date and time, parse it via a date time instance.api
  • .parseapi awaits a string value and converts it into a JS date (e.g. for storing it in a model).
  • .formatapi awaits a JS date and converts it into a string representation (e.g. for displaying it in the UI).

In Two Way data binding (Model-View-ViewModel pattern)

Similar to https://stackoverflow./a/63131534/5846045:

<DatePicker value="{
  path: '/MyDateTimeString',
  type: 'sap.ui.model.type.Date',
  formatOptions: {
    source: {
      pattern: 'yyyy-MM-ddTHH:mm:ss'
    }
  }
}" />

The framework will then use .format and .parse automatically. I.e. users can pick or enter a date in the UI, and the input will be then parsed and stored as a JS date in the model without writing a single line of JS code.

sap.ui.getCore().attachInit(() => sap.ui.require([
  "sap/ui/core/mvc/XMLView",
  "sap/ui/model/json/JSONModel", // sample model. Works also with ODataModel.
], (XMLView, MyModel) => XMLView.create({
  definition: `<mvc:View xmlns:mvc="sap.ui.core.mvc">
    <VBox xmlns="sap.m" class="sapUiTinyMargin">
      <DatePicker
        width="12rem"
        value="{
          path: '/MyDateTimeString',
          type: 'sap.ui.model.type.Date',
          formatOptions: {
            source: {
              pattern: 'yyyy-MM-ddTHH:mm:ss'
            }
          }
        }"
      />
      <ObjectAttribute
        title="Model"
        text="&#34;{/MyDateTimeString}&#34;"
      />
    </VBox>
  </mvc:View>`,
  models: new MyModel({ // e.g. v2.ODataModel
    MyDateTimeString: "2020-08-01T00:00:00", // Type="Edm.String"
  }),
}).then(control => control.placeAt("content"))));
<script id="sap-ui-bootstrap"
  src="https://openui5.hana.ondemand./resources/sap-ui-core.js"
  data-sap-ui-libs="sap.ui.core,sap.m"
  data-sap-ui-theme="sap_fiori_3"
  data-sap-ui-async="true"
  data-sap-ui-patversion="edge"
  data-sap-ui-xx-waitfortheme="init"
></script><body id="content" class="sapUiBody sapUiSizeCompact"></body>

Click "Show code snippet" and Run code snippet to see the demo in action.

The formater function expects a date object.

var Fdate = "2020-08-01T00:00:00";
var date = new Date(Fdate);
var dateFormat = sap.ui.core.format.DateFormat.getDateInstance({
    pattern: "dd/MM/yyyy"
});
fdateFormatted = dateFormat.format(date);
发布评论

评论列表(0)

  1. 暂无评论