I’m getting data from the form’s json when the user fills out the calendar
const data= JSON.stringify(orderForm.informationDate));
Currently, I get data from JSON to print PDF in any way:
{"year":2023,"month":12,"day":22}
How to change data to DD.MM.YYYY format?
4
Answers
you can get the attributes of this object (data.year, data.month, data.day) and concatenate into a string
${data.day}.${data.month}.${data.year}
Your question is based on a few misunderstandings:
Dates cannot be serialized to/from json – there is no date/time data type.
When you provide a date to the json serializer, you are providing an object, hence the serializer generates an object representation, like you saw:
The format that you mention in your question is clearly a string, since dates are typically stored internally as a number, not a string
In your case, you have two options:
JSON.stringify
:Keep in mind that you would/might need to reverse the process on the other side (e.g. to convert the string back to a date)
If you are using Datetime objects (and not a plain object), you can check the object class instead of the existence of day/month/year properties. This depends on your exact code.
It is recommended to not store the date in dd.mm.yyyy format but if you insist
2
becomes
02
.