简体   繁体   English

SAPUI5本地化日期验证

[英]SAPUI5 Localized Date Validation

Does anybody know which date formats SAPUI5 is using to validate localized dates with sap.m.DatePicker? 有人知道SAPUI5使用哪种日期格式通过sap.m.DatePicker验证本地化日期吗?

Example: When using sap.m.DatePicker with displayFormat "MMMM d, y" in en-US, I type "12/31/18" and it gets converted to "December 31, 2018", but when I type "12/31/2018" it doesn't seem to be a valid input. 示例:在美国使用带displayFormat“ MMMM d,y”的sap.m.DatePicker时,我键入“ 12/31/18”,它将转换为“ 2018年12月31日”,但是当我键入“ 12 / 31/2018”,这似乎不是有效的输入。

I figured it out! 我想到了! If there is no valueFormat provided for the sap.m.DatePicker, the input validation is based on the displayFormat and a couple of fallback formats which are specific to the given locale. 如果没有为sap.m.DatePicker提供valueFormat,则输入验证基于displayFormat和特定于给定语言环境的两个后备格式。 In en_US the fallback formats are: 在en_US中,后备格式为:

MMddyyyy MMddyyyy
MMddyy MMddyy
M/d/yy 年/月/日
MMM d, y MMM d,y
yyyy-MM-dd yyyy-MM-dd
yyyyMMdd yyyyMMdd
MMddyyyy MMddyyyy
MMddyy MMddyy

"12/31/2018" doesn't match any of those formats and therefore is not considered valid. “ 2018/12/31”与这些格式均不匹配,因此被视为无效。 I created this tool so you can check the locale specific formats and test some standard or custom display formats: http://jsbin.com/xalinujafi/edit?output 我创建了此工具,因此您可以检查特定于区域设置的格式并测试一些标准或自定义显示格式: http : //jsbin.com/xalinujafi/edit?output

Getting the DatePicker looks like this: 获取DatePicker如下所示:

var oDateFormat = sap.ui.core.format.DateFormat.getDateInstance({style: 'long'});
var oDate = new sap.m.DatePicker({
    width: '19.25rem',
    displayFormat: oDateFormat.oFormatOptions.pattern,
    change: function(oEvent){
        var oSource = oEvent.getSource();
        var oFormat = oSource._oDisplayFormat;
        if (!oFormat.parse(oSource.getValue())) {
            oSource.setValueState("Error");
            oSource.setValueStateText("Invalid Input");
        } else {
            oSource.setValueState("None");
            oSource.setValueStateText("");
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM