[英]Changing the format of date after picking from datepicker after SAPUI5
I have a datepicker from which I want to extract the date and display in a label. 我有一个日期选择器,我想从中选择日期并显示在标签中。 Currently the date is getting displayed in the format MM/DD/YYYY but I want it in the format MMM dd, yyyy (Nov 17, 2017). 当前日期正以MM / DD / YYYY格式显示,但我希望以MMM dd,yyyy格式(2017年11月17日)显示。 Below is the code : 下面是代码:
screenDate=view.byId("screeningDate").getValue();
var date = view.byId("__date");
date.setText(screenDate);
XML : XML:
<HBox alignItems="Center" renderType="Bare">
<Label text="Date of Screening" width="50%"/>
<DatePicker class="sapUiLargeMarginBegin" width="50%" id="screeningDate"/>
</HBox>
In addition to Naveen's answer here's the solution with your existing code: 除了Naveen的答案以外,这里还有您现有代码的解决方案:
screenDate=view.byId("screeningDate").getValue();
var date = view.byId("__date");
// Make date object out of screenDate
var dateObject = new Date(screenDate);
// SAPUI5 date formatter
var dateFormat = sap.ui.core.format.DateFormat.getDateInstance({pattern : "MMM dd,YYYY" });
// Format the date
var dateFormatted = dateFormat.format(dateObject);
date.setText(dateFormatted);
FYI, By getting view controls and updating the property in it is not good. 仅供参考,通过获取视图控件并更新其中的属性是不好的。 It will be good if you have a model. 如果您有模型,那将是很好。
Solution for your problem is below, 解决问题的方法如下
<Label text="{
path: '/date',
type: 'sap.ui.model.type.Date',
formatOptions: {
style: 'medium',
pattern: 'MMM dd, yyyy'
}}"/>
<DatePicker dateValue="{/date}"/>
And in the controller I have a JSONModel as below, 在控制器中,我有一个JSONModel,如下所示
onInit : function() {
this.model = new JSONModel({
date : new Date(0)
});
this.getView().setModel(this.model);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.