简体   繁体   English

如何使用javascript从字符串中提取日期

[英]How to extract date from string using javascript

How does one extract a date from a string using javascript? 如何使用javascript从字符串中提取日期? It can be in the following formats: 它可以采用以下格式:

31.07.2014 2014年7月31日

07.31.2014 2014年7月31日

2014.07.31 the same format but divided by spaces or / or - 31 07 2014 31/07/2014 31-07-2014 2014.07.31相同的格式但除以空格或/或 - 31 07 2014 31/07/2014 31-07-2014

the string may contain other character like 字符串可能包含其他字符

Teen.Wolf.S04E06.Orphaned.28.07.2014.HDTV Teen.Wolf.S04E06.Orphaned.28.07.2014.HDTV

so how to extract date from these type of name. 那么如何从这些类型的名称中提取日期。

I thought of first extracting all the numbers and then comparing if it is greater than 12 to make sure it is month or date. 我想先提取所有数字,然后比较它是否大于12,以确保它是月份或日期。 I don't know much about regEx (Regular Expressions) so if it is used please explain a little thank you 我对regEx(正则表达式)了解不多,所以如果使用的话请解释一下谢谢

probably use a regex like 可能使用正则表达式

/(\d{4}([.\-/ ])\d{2}\2\d{2}|\d{2}([.\-/ ])\d{2}\3\d{4})/

\d - a digit (equivilant to character class [0-9]
{n} - match n characters
[.\-/ ] - character class matches a single . - / or space (- needs to be escaped because it indicates a range in a character class
\n - a backreference matches the nth match so / will match another / and not a -, /, space or .

you can pull out the first part of the regex and inspect it, it is the same as the second part, except the 4 digits and 2 digits have been swapped 您可以拉出正则表达式的第一部分并检查它,它与第二部分相同,除了4位和2位数字已被交换

/\d{4}([.\-/ ])\d{2}\1\d{2}/

Maybe this could help you ( Demo Fiddle here ): 也许这可以帮到你( Demo Fiddle here ):

function getDate(d)
{
    var day, month, year;

    result = d.match("[0-9]{2}([\-/ \.])[0-9]{2}[\-/ \.][0-9]{4}");
    if(null != result) {
        dateSplitted = result[0].split(result[1]);
        day = dateSplitted[0];
        month = dateSplitted[1];
        year = dateSplitted[2];
    }
    result = d.match("[0-9]{4}([\-/ \.])[0-9]{2}[\-/ \.][0-9]{2}");
    if(null != result) {
        dateSplitted = result[0].split(result[1]);
        day = dateSplitted[2];
        month = dateSplitted[1];
        year = dateSplitted[0];
    }

    if(month>12) {
        aux = day;
        day = month;
        month = aux;
    }

    return year+"/"+month+"/"+day;
}

RegEX will help you to extract date with Different Type of format and It returns as Array, RegEX将帮助您使用不同类型的格式提取日期,并返回为Array,

let str="dd/mm/yyyy06/06/2018 yyyy/mm/dd 2018/02/12 d/m/yy 1/1/18 dd/mm/yy 18/12/12 mm/d/yyyy 12/2/2018 m/dd/yyyy 1/12/2018 yy/m/d 18/1/1 yy/mm/d 18/12/1 yyyy/2018/1/1";
str.match(/(\d{1,4}([.\-/])\d{1,2}([.\-/])\d{1,4})/g);

Reference Link From regextester 参考链接来自regextester

I think you can use regex for this. 我认为你可以使用正则表达式。 The main three expressions that you need are the following: 您需要的主要三个表达式如下:

[0-9]{4} // year
(0[1-9]|1[0-2]) // month
(0[1-9]|[1-2][0-9]|3[0-1]) // day

You can combine these to fit the formats you mentioned, for example, to match "31.07.2014": 您可以将这些组合起来以适合您提到的格式,例如,匹配“31.07.2014”:

(0[1-9]|[1-2][0-9]|3[0-1])\.(0[1-9]|1[0-2])\.[0-9]{4}

Or "31/07/2014": 或“31/07/2014”:

(0[1-9]|[1-2][0-9]|3[0-1])\/(0[1-9]|1[0-2])\/[0-9]{4}

You can decide which formats you need and create one regex expression separating the formats with the OR operator |. 您可以决定所需的格式,并创建一个使用OR运算符分隔格式的正则表达式。

function myFunction() {
    var str = "Teen.Wolf.Orphaned.28.07.2014.HDTV";
    var res = str.split(".");


    var text = "";
    var x;
    for (x in res) {
        if (!isNaN(res[x])) {
            text += res[x];
            if (text.length == 2) { text += ','} 
            else if (text.length == 5) { text += ',' } 
        }
    }

    document.write(text);
}

This will write "28,07,2014" 这将写成“28,07,2014”

NOTE: only use this way if the strings will always be in a format similar to the ones you posted above. 注意:如果字符串的格式与您上面发布的格式类似,则只能使用这种方式。

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

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