Skip to content

Instantly share code, notes, and snippets.

@Gaurav8757
Last active May 27, 2024 13:49
Show Gist options
  • Save Gaurav8757/57d3b4356127c1e05c76c1875cf977b3 to your computer and use it in GitHub Desktop.
Save Gaurav8757/57d3b4356127c1e05c76c1875cf977b3 to your computer and use it in GitHub Desktop.
YYYY-MM-DD to DD-MM-YYYY
import React, { useState } from 'react';
function AddTerminator() {
const [terminatedate, setTerminateDate] = useState("");
const [dateInput, setDateInput] = useState("");
const convertDateFormat = (dateStr) => {
const [year, month, day] = dateStr.split('-');
return `${day}-${month}-${year}`;
};
const handleInputChange = (event) => {
const input = event.target.value;
if (/^\d{4}-\d{2}-\d{2}$/.test(input)) {
const formattedDate = convertDateFormat(input);
setTerminateDate(formattedDate);
} else {
setTerminateDate(''); // Clear the converted date if the format is invalid
}
setDateInput(input);
};
return (
<div>
<input
className="input-style p-1 text-base rounded-lg"
type="date"
value={dateInput}
placeholder="dd-mm-yyyy"
onChange={handleInputChange}
/>
{terminatedate && <p>Formatted Date: {terminatedate}</p>}
</div>
);
}
export default AddTerminator;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment