5

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 divided by spaces or / or - 31 07 2014 31/07/2014 31-07-2014

the string may contain other character like

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. I don't know much about regEx (Regular Expressions) so if it is used please explain a little thank you

BluSky
  • 63
  • 1
  • 1
  • 7
  • 5
    Have you looked at [MomentJS](http://momentjs.com/)? It's a great library for dealing with dates/times. – Lix Jul 31 '14 at 13:32
  • Thank you, will be reading it. Is it possible to write it with plain javascript without making use of those library – BluSky Jul 31 '14 at 13:35
  • It's possible to write the dates in format YYYY MM DD ? – Mario Araque Jul 31 '14 at 13:38
  • 2
    it can make confusion if both 2 first numbers are lower than 12 – Khalid Jul 31 '14 at 13:40
  • 1
    How would one know if `01.02.2014` is January 2nd or February 1st? – Qtax Jul 31 '14 at 13:43
  • Actually i want to extract date from tv shows file name, which looks something like this "Teen.Wolf.S04E06.Orphaned.(28.07.2014).HDTV", date format may change to yyyy-mm-dd or any other type as mentioned – BluSky Jul 31 '14 at 13:44
  • you can use this to get the number in () http://stackoverflow.com/questions/17779744/regular-expression-to-get-a-string-between-parentheses-in-javascript – aahhaa Jul 31 '14 at 13:46
  • @Qtax, mostly america people use mm-dd-yyyy format but indian use dd-mm-yyyy so its quite tricky, lets say i want to do the american way – BluSky Jul 31 '14 at 13:49

5 Answers5

8

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}/
Nick
  • 158
  • 1
  • 7
  • Couldn't able to upvote brother, cause i don't have 15 reps. I was wondering if i can store date, month and year in different variables – BluSky Jul 31 '14 at 14:15
6

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;
}
lpg
  • 4,839
  • 1
  • 14
  • 15
3

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

Venkatesan
  • 138
  • 1
  • 10
1

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

Vlad Cazacu
  • 1,461
  • 12
  • 12
  • The date break down was useful, thanks! I've used it to produce a regex for the date format YYYY-MM-DD: `var regex = /([0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]))/g;` Notice that all date segments were wrapped with parentheses because the string I'm matching with has another date format!. – Cortex May 28 '20 at 22:31
0
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.

Jack C
  • 1,014
  • 2
  • 10
  • 22