I have two input fields:
<input type="date" id="currentDate"> <input type="time" id="currentTime">
How can I set these fields to the current date/time?
Answers. //Get value of input text var dt = $("input[type=text]"). val(). split('-'); var date = dt[2] +"-"+ dt[1] +"-"+ dt[0];
input type="time" The time input type is used to create input fields allowing the users to easily enter the time (hours and minutes, sometimes seconds). The value of the time input type is commonly in 24-hour format ("hh:mm").
You can just set the value
of the input field with the respective formats:
date
is yyyy-MM-dd
time
is HH:mm
Using your example, you can do something simple like:
var date = new Date(); var currentDate = date.toISOString().substring(0,10); var currentTime = date.toISOString().substring(11,16); document.getElementById('currentDate').value = currentDate; document.getElementById('currentTime').value = currentTime;
var date = new Date(); var day = date.getDate(), month = date.getMonth() + 1, year = date.getFullYear(), hour = date.getHours(), min = date.getMinutes(); month = (month < 10 ? "0" : "") + month; day = (day < 10 ? "0" : "") + day; hour = (hour < 10 ? "0" : "") + hour; min = (min < 10 ? "0" : "") + min; var today = year + "-" + month + "-" + day, displayTime = hour + ":" + min; document.getElementById('formdate').value = today; document.getElementById("formtime").value = displayTime;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With