i am trying to create a script that can update the current time automatically in the datetime-local input without having to click and selecting the current time. this is the code that i am working with to have the input display the current date and time, but i also want it to update the current time when the time changes.
then i hope to submit the datetime-local value to the database using PHP (i got this section covered)
and i was hoping if it's possible to delete the date and time selection button in the input and how can i show the seconds, but have the seconds running accurately with current time
thank you all.
var now = new Date();
now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
document.getElementById('dt').value = now.toISOString().slice(0,16);
<input id="dt" type="datetime-local" />
I think you can set an interval every second for this.
functionName(); //First execution
setInterval(functionName, 1000); //Check every second
function functionName(){
var now = new Date();
now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
document.getElementById('dt').value = now.toISOString().slice(0,16);
}
<input readonly id="dt" type="datetime-local" />
Of course you will need to "Stop" this interval when the user clicks on the input, or the date can never be changed
EDIT: For avoid the user to change the date, just add readonly attribute to the input tag.