【问题标题】:Interpret user entered dates in JavaScript解释用户在 JavaScript 中输入的日期
【发布时间】:2013-04-06 17:02:09
【问题描述】:

我正在寻找一种方便的方法来获取用户输入的日期并执行以下操作:

1) 判断输入的Date是否有效,如果有效,返回一个对象:

2) 一个 JavaScript 日期对象

3) mySQL 格式的日期 (YYYY-MM-DD)

4) 以典型格式(MM-DD-YYYY)格式化的日期

最后,我编写了自己的函数,该函数使用正则表达式,可以处理 YYYY-MM-DD、YYYY/MM/DD、MM-DD-YY、MM/DD/YY、MM-DD-YYYY 或月/日/年。

函数在对象中返回mySQL格式和正则格式的原因只是为了方便。我的 Web 应用需要在字段中显示典型格式,在保存数据时需要将 mySQL 格式发送到服务器。

代码如下所示。我确信有一些方法可以优化代码,但为了便于阅读,我分部分写。即使它被设置为每次用户在日期字段中输入数据时运行,它也不会陷入困境。希望这可以帮助某人!

【问题讨论】:

    标签: javascript date input format interpreter


    【解决方案1】:

    更新:momentjs 好多了。

    可以在jsfiddle 上看到代码和示例。

    function interpretDate(stringDate){
        var mysqlF = "(\\d{4})[-/](\\d{1,2})[-/](\\d{1,2})";
        var dispF = "(\\d{1,2})[-/](\\d{1,2})[-/]((?:\\d{4})|(?:\\d{2}))";
        var dispNoYearF = "(\\d{1,2})[-/](\\d{1,2})";
        var dateFormats = "(^"+mysqlF+"$)|(^"+dispF+"$)|(^"+dispNoYearF+"$)";
    
        //Let's try to extract the data
        data = stringDate.match(dateFormats);
        var month = -1;
        var day = -1;
        var year = -1;
    
        //Check to see if the verification failed
        if (data == undefined){
            //Invalid date
            return {valid: false, date: null, mysqlDate:null, displayDate: ""};
        }
    
        //Extract the data based on the entry type
        if (data[1] != undefined){//YYYY-MM-DD
            month = parseInt(data[3]);
            day = parseInt(data[4]);
            year = parseInt(data[2]);
        }else if (data[5] != undefined){//MM-DD-YYYY or MM-DD-YY
            month = parseInt(data[6]);
            day = parseInt(data[7]);
            year = parseInt(data[8]);
            if (year < 100){
                var yearString = new String(new Date().getFullYear());
                year = parseInt(yearString.substr(0,2) + year);
            }
        }else if (data[9] != undefined){//MM-DD
            month = parseInt(data[10]);
            day = parseInt(data[11]);
            year = parseInt(new Date().getFullYear());
        }
    
        //If we are here, we have three numbers, let's see if they make a real date
        var extractedDate = new Date(year, month-1, day);
        if (extractedDate.getFullYear() != year || extractedDate.getDate() != day || extractedDate.getMonth() != (month-1)){
            return {valid: false, date: null, mysqlDate:null, displayDate: ""};
        }
    
        //We have a valid date, let's add front zeros
        var monthFixed = month;
        if (monthFixed < 10) monthFixed = "0"+monthFixed;
        var dayFixed = day;
        if (dayFixed < 10) dayFixed = "0"+dayFixed;
    
        //We are done
        return {valid: true, date: extractedDate, mysqlDate:year+"-"+monthFixed+"-"+dayFixed, displayDate: month+"/"+day+"/"+year};
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-27
      • 2021-12-03
      • 2018-04-25
      • 2018-02-06
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多