首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >jQuery中不含DateJS的解析日期

jQuery中不含DateJS的解析日期
EN

Stack Overflow用户
提问于 2011-02-16 16:54:01
回答 2查看 3.3K关注 0票数 4

我正试图解析所有月份的格式January 1, 1900February 1, 1900等的日期。然后将月、日和年分离成它们自己的对象。

我试过使用开箱即用的正则表达式,但是:

  1. 这个特定的正则表达式看起来太复杂了,就像它很容易破坏
  2. 一样,必须有一个更容易使用的正则表达式,知道格式不会改变(我们将在后端验证日期)

我不想使用DateJS库,因为它似乎包含了大量的代码来解析一个日期,那么是否有更简单的方法来编写正则表达式呢?除了执行正则表达式或DateJS之外,还有其他路线吗?

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

代码语言:javascript
运行
复制
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;
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-16 18:21:59

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

代码语言:javascript
运行
复制
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;
}

正如您所看到的,这个函数非常简单,应该在所有现代浏览器中得到支持。

票数 5
EN

Stack Overflow用户

发布于 2011-02-16 17:38:50

这将是可行的,但不会是特定的几个月和几年。它只需要3-9个字母,一个或两个数字,一个逗号和4个数字.

代码语言:javascript
运行
复制
/^[a-z]{3,9} [0-9]{1,2}, [0-9]{4}$/i
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5019448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档