How from such a string with the date dd.mm.yyyy to get an object with a date, such a type, how does new Date() return?

    1 answer 1

    If the format is always one ( dd.mm.yyyy ), then you can use a regular expression to bring it into a "valid" form, for example 10/20/2018> 2018/10/20, and transfer such a string to new Date () . At the output we get the format that you need.

     let mydate = '20.10.2018'; mydate = new Date(mydate.replace(/(\d+).(\d+).(\d+)/, '$3/$2/$1')); console.log(mydate.toDateString()); // Sat Oct 20 2018 

    Or, alternatively, through split () .
    But pay attention to mydate[1] - 1 . A month with such a transfer to the Date counts from 0 (January - 0, etc.,) , therefore, we subtract a unit from 10 and get the month we need.

     let mydate = '20.10.2018'.split('.'); mydate = new Date(mydate[2], mydate[1] - 1, mydate[0]); console.log(mydate.toDateString());