Last active
March 10, 2026 11:46
-
-
Save Gaurav8757/10395685cabd994d3148287544585988 to your computer and use it in GitHub Desktop.
Date dd-MM-yyyy dikhe input fields mein but api pe dd/MM/yyyy jaye/save ho
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // formatDate helper functions | |
| const formatDate = (dateString, format = "dd/MM/yyyy") => { | |
| if (!dateString) return ""; // Handle empty date | |
| const date = new Date(dateString); | |
| if (isNaN(date.getTime())) return "Invalid Date"; // Check if date is valid | |
| const day = String(date.getDate()).padStart(2, "0"); | |
| const month = String(date.getMonth() + 1).padStart(2, "0"); // Months are zero-based | |
| const year = date.getFullYear(); | |
| if (format === "yyyy-MM-dd") { | |
| return `${year}-${month}-${day}`; // For input field | |
| } | |
| return `${day}/${month}/${year}`; // For Redux | |
| }; | |
| export default formatDate; | |
| // User form code | |
| const [selectedDate, setSelectedDate] = useState(formatDate(new Date(), "yyyy-MM-dd")); // alg state lenge default value today ho | |
| const handleDateChange = (e) => { | |
| const dateValue = e.target.value; | |
| setSelectedDate(dateValue); // // input date mein yyyy-MM-dd ja raha h | |
| const formattedDateForRedux = formatDate(dateValue, "dd/MM/yyyy"); // api mein dd/MM/yyyy ja raha h | |
| dispatch( | |
| magmaQuotes({ field: "ProposalDate", value: formattedDateForRedux }) // api mein dd/MM/yyyy ja raha h | |
| ); | |
| setValue("ProposalDate", formattedDateForRedux); | |
| }; | |
| <input | |
| type="date" | |
| onChange={handleDateChange} | |
| value={selectedDate} | |
| className={`flex w-full border rounded ${ | |
| methods.formState.errors?.ProposalDate ? "border-red-500" : "border-gray-300"}`}/> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment