create new date form string 13.11.2022 in javascript

You can use the Date() constructor in JavaScript to create a new Date object from a string. Here's how you can create a new date from the string "13.11.2022":

index.tsx
const dateString = "13.11.2022";
const parts = dateString.split(".");
const date = new Date(parts[2], parts[1] - 1, parts[0]);
console.log(date);
146 chars
5 lines

We first split the string using the . separator and then pass the resulting array's elements to the Date() constructor. The Date() constructor takes in the year, month (0-indexed), and day as its arguments.

Note that we subtract 1 from the month because Date() expects the month to be 0-indexed (0 = January, 1 = February, 2 = March, and so on).

The above code will output the following:

index.tsx
Sat Nov 13 2022 00:00:00 GMT+0000 (Coordinated Universal Time)
63 chars
2 lines

You can then format this date object using the various methods provided by the Date object, such as .toString() or .toLocaleString().

gistlibby LogSnag