【问题标题】:Parsing date in jQuery without DateJS在没有 DateJS 的情况下在 jQuery 中解析日期
【发布时间】:2011-06-28 12:19:53
【问题描述】:

我正在尝试为所有月份解析格式为 January 1, 1900February 1, 1900 等的日期。然后将月份、日期和年份分隔到它们自己的对象中。

我已尝试为此使用开箱即用的正则表达式,但是:

  1. 这个特殊的正则表达式似乎过于复杂,而且很容易破坏
  2. 必须有一个更容易使用的正则表达式,知道格式不会改变(我们将在后端验证日期)

我不想使用 DateJS 库,因为它似乎包含很多代码来解析一个日期,那么有没有更简单的方法来为此编写正则表达式?除了做正则表达式或者DateJS,还有其他的路线吗?

无论出于何种原因,正则表达式在二月份不起作用,并且您可以看到它在一个数组中返回了很多对象,而如果它只返回 3 个对象(月、日、年)显然会更容易。这是我使用正则表达式编写的当前函数...:

function convertDate(dateString) {
    // must be in the format MMMMMMM DD, YYYY OR MMM DD, YYYY
    // examples: January 1, 2000 or Jan 1, 2000 (notice no period for abbreviating January into Jan)
    var dateRegex = new RegExp('^(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?|Dec(ember)?)\\ 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?)\\ (0?[1-9]|([12]\\d)|30))|(Feb(ruary)?\\ (0?[1-9]|1\\d|2[0-8]|(29(?=,\\ ((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))))\\,\\ ((1[6-9]|[2-9]\\d)\\d{2}))');
    var fullDate = dateString.match(dateRegex);
    console.log(fullDate);

    if (fullDate) {
        var month = fullDate[12];
        var day = fullDate[24];
        var year = fullDate[35];

        if (month == 'January' | month == 'Jan') { integerMonth = 1; } 
        else if (month == 'February' | month == 'Feb') { integerMonth = 2; } 
        else if (month == 'March' | month == 'Mar') { integerMonth = 3; } 
        else if (month == 'April' | month == 'Apr') { integerMonth = 4; } 
        else if (month == 'May') { integerMonth = 5; } 
        else if (month == 'June' | month == 'Jun') { integerMonth = 6; } 
        else if (month == 'July' | month == 'Jul') { integerMonth = 7; } 
        else if (month == 'August' | month == 'Aug') { integerMonth = 8; } 
        else if (month == 'September' | month == 'Sep') { integerMonth = 9; } 
        else if (month == 'October' | month == 'Oct') { integerMonth = 10; } 
        else if (month == 'November' | month == 'Nov') { integerMonth = 11; } 
        else if (month == 'December' | month == 'Dec') { integerMonth = 12; } 

        return {month : integerMonth, day : day, year : year}
    } else {
        return false;
    }
}

【问题讨论】:

    标签: javascript jquery regex parsing date


    【解决方案1】:

    Javascript Date 对象可以用字符串初始化,它会将您使用的格式解析为正确的日期:

    var d = new Date("January 1, 2000");
    if (!isNaN(d.getMonth()) { // check for invalid date
      return {month : d.getMonth()+1, day : d.getDate(), year : d.getFullYear()};
    } else {
      return false;
    }
    

    如您所见,这个功能相当简单,应该在所有现代浏览器中都支持。

    【讨论】:

    • 简单得多,没有正则表达式,甚至可以检查闰年和一个月中的正确日期......谢谢!
    【解决方案2】:

    这会起作用,但不会特定于几个月和几年。它只需要 3-9 个字母、一个或两个数字、一个逗号和 4 个数字。

    /^[a-z]{3,9} [0-9]{1,2}, [0-9]{4}$/i
    

    【讨论】:

    • 感谢纳鲁姆!有没有办法将元素分离成一个数组?目前如果我这样做:var fullDate = dateString.match(/^[a-z]{3,9} [0-9]{1,2}, [0-9]{4}$/i); 我只得到一个返回值,它是整个日期而不是“一月”“1”“1900”
    • 啊,没关系。我想出了反向引用,所以现在我有了:var fullDate = dateString.match(/^([a-z]{3,9}) ([0-9]{1,2}), ([0-9]{4})$/i); 并将它们放入一个数组中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多