Skip to content Skip to sidebar Skip to footer

Have Time Automatically Be In The Input Type="time"

I am trying to display the current time as the value in my input upon loading the page. However, its not working for some reason. Any suggestions? Thanks for any help.

Solution 1:

Use

var now = new Date();

Instead of

 var current = new Date();

Solution 2:

Use valueAsDate to make the input display the value

  <body>
      <input type="time" id="theTime">
      <script>
          $(document).ready( function() {
              var now = new Date();
              //now2 prevents the milliseconds from showing up in the input
              var now2 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds());
              $('#theTime')[0].valueAsDate = now2;
          });
      </script>
  </body>

Solution 3:

You are using wrong variable use current instead,

$(document).ready( function() {
          var current = new Date();
          var minute = (current.getMinutes() + 1);               
          var seconds = current.getSeconds();
          if(minute < 10) 
              minute = "0" + minute;
          if(seconds < 10) 
              seconds = "0" + seconds;
          var today = current.getFullYear() + '-' + minute + '-' + seconds;
          $('#theTime').val(today);
         });

See the working code here


Solution 4:

<!DOCTYPE html>
<html>
   <head>
      <meta charset="UTF-8">
      <script src="Scripts/Global.js"></script>
      <script src="Scripts/jquery.js"></script>   
      <link rel="stylesheet" type="text/css" href="Styles/Design.css">
   </head>
   <body>
      <input type="time" id="theTime">
      <script>
         $(document).ready(function() {
          let now = new Date();
          $('#theTime').val(now.getHours()+":"+ now.getMinutes());
         });
      </script>
   </body>
</html>        

Post a Comment for "Have Time Automatically Be In The Input Type="time""