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.tsx146 chars5 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.tsx63 chars2 lines
You can then format this date object using the various methods provided by the Date
object, such as .toString()
or .toLocaleString()
.
gistlibby LogSnag