How to extract date from string using javascript

后端 未结 5 1467
轮回少年
轮回少年 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.

提交回复
热议问题