Answer the question
In order to leave comments, you need to log in
How to convert date format taken from DB to Javascript?
Hello colleagues.
I have a date format in the database, (field with date format) for example 20-05-202 1 (May 5, 2021). I want this format D-M_Y (D-day, M-month, Y-year --> well, it's purely, right now I came up with such an inscription, it has nothing to do with the format). I need to convert it to May 20 (21) . How can I do that?
Answer the question
In order to leave comments, you need to log in
You can work like with text: split into an array, hyphen separator. Leave the day as it is, convert the month to a number and take the name from the manually entered array of all months. Take only the last two digits of the year. Glue.
How did you try? You can use standard Intl
formatting , but it's without parentheses. For example:
const formatDate = (str) => new Intl.DateTimeFormat('ru-RU', {year: '2-digit', month: 'short', day: 'numeric'})
.format(new Date(str.split('-').reverse().join('-')));
formatDate('20-05-2021') // "20 мая 21 г."
Go to learnjs there is an exhaustive answer on how to work with the date, in short, you create a format description and pass it when formatting
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question