在JS中,我们可以使用new Date(),来得到前端的日期(时间),但此得到的时间格式化为如:Fri Jan 22 2021 15:08:32 GMT+0800 (中国标准时间)
这数据使用起来明显不太方便,我们可能会想得到标准的SAP格式20210122,又或者2021-01-22等等。所以我们就需要在得到一个日期对像时进行转换,或者把一个字符串转换成日期对像。
1.把日期对像按格式转换为字符串显示
以下为了说明,定义了两个INPUT输入框,一个显示日期对像__datum,一个显示格式化化为YYYY-MM-DD的字符串。在这里使用的DateFormat,需要在JS头中引用”sap/ui/core/format/DateFormat”。
1 2 3 4 5 6 7 8 9 |
var oFormatYyyymmdd = DateFormat.getInstance({pattern: "yyyy-MM-dd"}); var toDay = oFormatYyyymmdd.format(new Date()); this.byId("__datum").setValue(new Date()); this.byId("__budat").setValue(toDay); |
在代码调试时,我们就能得到,TODAY 的值为:2021-01-22.
thanks