[英]How to convert datetime string to date in SAPUI5?
I am trying to convert String to Date in SAPUI5我正在尝试在 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未捕获的类型错误:j.getTime 不是 function
I tried to set the source pattern as "yyyy-MM-ddTHH:mm:ss".我尝试将源模式设置为“yyyy-MM-ddTHH:mm:ss”。 But this returns the same error.但这会返回相同的错误。
// 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"
.parse
api awaits a string value and converts it into a JS date (eg for storing it in a model). .parse
api等待字符串值并将其转换为 JS 日期(例如用于将其存储在模型中)。.format
api awaits a JS date and converts it into a string representation (eg for displaying it in the UI). .format
api等待 JS 日期并将其转换为字符串表示形式(例如,用于在 UI 中显示)。Similar to https://stackoverflow.com/a/63131534/5846045 :类似于https://stackoverflow.com/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.然后框架将自动使用.format
和.parse
。 Ie 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.即用户可以在UI中选择或输入一个日期,然后输入的内容会被解析并作为JS日期存储在model中,而无需编写一行JS代码。
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=""{/MyDateTimeString}"" /> </VBox> </mvc:View>`, models: new MyModel({ // eg 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.com/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-compatversion="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.格式化程序 function 预计日期为 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.