skip to Main Content

How to auto fill this two input without inputting?

<input type="time" name="Time" id="Time" value="" />

I want to automatically fill date and time without inputting according to the desktop date and time.

3

Answers


  1. You can fill almost any input with its value property.

    In javascript:

    document.querySelector('#Time').value = '07:30'
    

    For it to consume your system time create a date object and feed the hours and minutes to the input

    const now = new Date();
    document.querySelector('#Time').value = `${now.getHours()}:${now.getMinutes()}`
    
    Login or Signup to reply.
  2. You can do by javascript.

    <input type="date" name="Date" id="Date" value="" />
    <input type="time" name="Time" id="Time" value="" />
    
    <script>
      //get current date-time of system like Desktop , Server etc....
      const now = new Date();
    
      //format date and time as string types
      const dateStr = now.toISOString().slice(0, 10);
      const timeStr = now.toTimeString().slice(0, 5);
    
      //set values inside HTML tags
      document.getElementById('Date').value = dateStr;
      document.getElementById('Time').value = timeStr;
    </script>
    
    Login or Signup to reply.
  3. The code for HTML:

    <input type="time" id="time_">
    var now = new Date();
    var current_time  = `${now.getHours()}:${now.getMinutes()}`
    document.querySelector('#time_').value = current_time;
    <input type="time" id="time_">

    Here is the code for getting current time using javascript:

    var now = new Date();
    var current_time  = `${now.getHours()}:${now.getMinutes()}`
    

    If you want to add this using javascript then:
    document.querySelector(‘#time_’).value = current_time;

    And if you want this in jquery then:

    $("#time_").val(current_time);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search