I have a Node.js and MySQL project. The user inputs the date from a dropdown and submits the form. In the backend, I see proper date but when I fetch it, its 1 day behind. I've read that it might be a UTC problem but I don't understand how to fix it. I have some custom JavaScript for this to avoid user input.
HTML:
<div class="col-4">
<label for="dob" class="form-label">Date of Birth</label>
<input type="date" class="form-control" id="dob" name="dob" required value="{{formData.dob}}">
<div class="invalid-feedback">Please enter your date of birth.</div>
</div>
JavaScript:
// Set limit
const dateInput = document.getElementById("dob");
const maxDate = new Date().setFullYear(new Date().getFullYear() - 18);
dateInput.setAttribute("max", new Date(maxDate).toISOString().split('T')[0]);
dateInput.addEventListener("keydown", function(event) {
event.preventDefault(); // Prevent typing
});
dateInput.addEventListener("input", function(event) {
event.preventDefault(); // Prevent input
});
The current input if given as 2005-01-05
, returns as 2005-01-04T18:30:00.000Z
did some digging around and wrote this code that now works for my project. thanks to all that helped!
const dob = "{{user.dob}}"; const localDob = new Date(dob);
const formattedDob = localDob.toLocaleDateString();
document.getElementById('dobDisplay').textContent = formattedDob