How to extract date from string using javascript

后端 未结 5 1458
轮回少年
轮回少年 2021-01-21 11:35

How does one extract a date from a string using javascript? It can be in the following formats:

31.07.2014

07.31.2014

2014.07.31 the same format but divi

相关标签:
5条回答
  • 2021-01-21 12:09
    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"

    NOTE: only use this way if the strings will always be in a format similar to the ones you posted above.

    0 讨论(0)
  • 2021-01-21 12:11

    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":

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

    Or "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 |.

    0 讨论(0)
  • 2021-01-21 12:14

    RegEX will help you to extract date with Different Type of format and It returns as 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

    0 讨论(0)
  • 2021-01-21 12:16

    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

    /\d{4}([.\-/ ])\d{2}\1\d{2}/
    
    0 讨论(0)
  • 2021-01-21 12:23

    Maybe this could help you (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;
    }
    
    0 讨论(0)
提交回复
热议问题