D
D
Danya Secret2020-08-12 17:49:52
JavaScript
Danya Secret, 2020-08-12 17:49:52

How to reformat the date in the desired form?

Through the field, the date from the field gets into js (not a field specifically for the date, but a regular input). And there may be different forms, but for the api you need to reduce everything to YYYY-MM-DD (the api is not mine, I can’t change it). How to implement this in js without using a calendar?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sakhnyuk, 2020-08-12
@sakhnyuk

The simplest thing is to include the moment library (or an analogue of day-fns, etc.)
to get the date in the required format:

const formatedDate = moment(sourceDate).format('YYYY-MM-DD')

Also, to set the format for parsing the date (if a string comes from the input), you need to pass the string with the format as the second argument to the moment function: If it is required without a moment, then write your own implementation using the Date class
const date = moment(dateString, 'DD-MM-YYYY')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question