You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hii I am working on a project where i have to fetch dates from SQL database and show it in dd-mm-yyyy format.
But for some reason the dates are stored as strings in database and some values are NULL and some are not valid date strings.
for NULL values I have to return a empty string as they are intentionally NULL.
But my main problem is that format function in date-fns only accept Date datatype as parameter and throws error for non-Date datatype parameters.
so as i am passing my values to format function it crashes my app as some values as they are not valid.
so i have to first check that dates are valid. for that I am doing something like this
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hii I am working on a project where i have to fetch dates from SQL database and show it in dd-mm-yyyy format.
But for some reason the dates are stored as strings in database and some values are NULL and some are not valid date strings.
for NULL values I have to return a empty string as they are intentionally NULL.
But my main problem is that format function in date-fns only accept Date datatype as parameter and throws error for non-Date datatype parameters.
so as i am passing my values to format function it crashes my app as some values as they are not valid.
so i have to first check that dates are valid. for that I am doing something like this
const formated_date = date_string ? isValid(new Date(date_string)) ? format(new Date(date_string), 'DD-MM-YYYY') : 'Invalid date' : ' '
see that i am casting my strings to Date always, and the same in moment looks like
const formated_date = date_string ? moment(date_string).format('DD-MM-YYYY') : ' '
I think the moment version is more readable and easy to read when you later go through your code.
so is there a easy way to format dates in date-fns and not crash for invalid dates.
Thanks .
Beta Was this translation helpful? Give feedback.
All reactions