I created a form using antd proform with 4 fields. The first field is an input number named interval, the second is a select field with 2 options: "day(s)" and "month(s)", the third and fourth are datepicker fields. For example, if the user enters 30 as an interval, selects "days", and selects 2023-08-10 as the last due date, the next due date should be calculated automatically and displayed (2023-09-09) in the fourth field. How can I do this?
Here is my code:
<ProForm.Group>
<ProFormDigit onChange={(value) => setIntervalDays(value)} name="interval" label="Interval" width="sm" />
<ProFormSelect
onChange={(value) => setKindOfDate(value)}
name="kindOfDate"
label=""
width="md"
options={[
{
value: "1",
label: "Day(s)",
},
{
value: "2",
label: "Month(s)",
},
]}
/>
</ProForm.Group>
<ProForm.Group>
<ProFormDatePicker onChange={(value) => setLastDueDate(value)} name="lastDueDate" label="Last Due Date" />
<ProFormDatePicker onChange={(value) => setNextDueDate(value)} name="nextDueDate" label="Next Due Date" />
</ProForm.Group>
2
Answers
First of all, the Date object of javascript isn’t the best (in my opinion), you have better libraries that can do the work in such a great way.
Let’s say the form part is done (get user input values).
You can do it with Date object like this :
Or, you can simply add "moment" library to your project and use this function :
Hope it helps, i can help you more if you need to fetch values and call the function correctly.
The Date object built into Javascript is not too hard to use.
There are libs out there for manipulating / formatting a bit easier. But if all your after is adding 30 days to another date, you can just use
Date.setDate / getDate
to achieve this.eg.