To create a new date from a string '13.11.2022', we can use the Date constructor with arguments for the year, month, and date.
Example 1: Using plain JavaScript
index.tsx146 chars5 lines
In this example, we split the string on the '.' separator, then create a new Date object with the year, month, and date. Note that we subtract 1 from the month value as JavaScript months are zero-indexed (i.e. January is 0, February is 1, etc.)
Example 2: Using moment.js library
index.tsx108 chars4 lines
In this example, we use the moment.js library to parse the date string and convert it to a Date object. The 'DD.MM.YYYY' argument specifies the format of the input string.
Example 3: Using Date.parse method
index.tsx119 chars4 lines
In this example, we use the Date.parse method to create a new date object from the string. We first replace all '.' characters with '-' to ensure that it is in a format that can be parsed by the method. Then we pass this modified string to the Date constructor.
gistlibby LogSnag