document.addEventListener("DOMContentLoaded", function() { var inputDate = document.querySelector("form input[name=\"fictionalDate\"]") var pError = document.querySelector("form p.red") if (inputDate.value != "") { inputDate.value = new Date(inputDate.value).toDateString().substring(4) } document.querySelector("form").onsubmit = function(ev) { console.log(inputDate.value) var date = new Date(inputDate.value + " UTC") if (Number.isNaN(date.getTime())) { date = new Date(inputDate.value) if (Number.isNaN(date.getTime())) { pError.innerHTML = "Invalid date" return false } } inputDate.value = date.toISOString() return true } })