When working with a string that contains a date in the format 'DD-MM-YYYY' and wanting to change it to 'DD/MM/YYY', it is common practice to use the split method for formatting.
The split method successfully separates the string and formats the date as desired, which seems to work fine initially.
However, an issue arises when an error is thrown indicating that .split() is not a function.
Attempts to troubleshoot by changing the method to multiline for debugging purposes and confirming the data type of the string being split show that the variable is indeed a string, yet the same error persists.
formatDate(date) {
if (!date) return null
const [year, month, day] = date.split('-')
return `${day}/${month}/${year}`
}
The formatDate function expects a string argument like ('29-09-2018').
Expected:
To format the 'date' without encountering any errors in the console.
Actual:
Although the 'date' is formatted, an error message appears:
vue.runtime.esm.js?2b0e:1888 TypeError: date.split is not a
function