Put the time to input text in view html with jquery (not Php)

2

How can I put today's date by default to an input text when executing my view in html with jquery javascript? The only thing I have achieved is this code, which works but does not print in the input but in the body of the view:

    <script>
    var f = new Date();
document.write(f.getDate() + "/" + (f.getMonth() +1) + "/" + f.getFullYear());
    
</script>

this shows the date loose in the body of the view, now if I want to paint it in the input and try this, it becomes a kind of loop and na does not show my form just the date, as shown in the image:

    <script>
    $("#FechaActual").ready(function){
    var f = new Date();
document.write(f.getDate() + "/" + (f.getMonth() +1) + "/" + f.getFullYear());
    
    });    

</script>

    
asked by Juan Fernando Dj Jf 01.04.2018 в 11:56
source

2 answers

0
  

I'll give you the following example, where you can notice that to modify   the value of an input and showing the date, for example; you have it   do through the value method.

  • In the date variable, I create a new instance of new Date()
  • now with the variable date access to getFullYear() to obtain the year
  • now with the variable date access to getMonth() to get the month
  • now with the variable date access to getDate() to get the day
  •   

    To insert the dynamic value into the input, first I get it to   through its id and assign it to the variable sign ; then I do   that variable sign accesses the value method and I declare that this is   equal to my day, month and year

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    </head>
    <body>
      <input type="text" id="fecha">
    <script>
      const letrero = document.querySelector("#fecha")
    
      const fecha = new Date();            
      let anio = fecha.getFullYear()
      let mes = fecha.getMonth()+1
      let dia = fecha.getDate()
    
    
      if(dia < 10){
      dia='0'+dia
      }
      if(mes < 10){
      mes='0'+mes
      }
      letrero.value = '${dia} / ${mes} / ${anio}'
      
    </script>
    </body>
    </html>
      

    The use of template strings is to be able to print dynamic values   with text strings without using the plus sign; What you need is the   use of inverted quotes and dynamic values or better   known as variables go with $ and between simple keys

         

    As notes I add one to the month, this happens because JS reads the months   as an array and start from zero position, then you have to   add one to match the month in which you are running the code

    I added only two conditionals (if), so that the day and month is less than 10 I added a zero at the beginning and instead of reading 4/4/2018 , we read > 04/04/2018

        
    answered by 04.04.2018 / 21:34
    source
    1

    Assuming you have an html structure ja implemented, where you have a tag for the input text with a specific ID.
    In this example I am assuming that the ID that is assigned to the input text is input_date.

    He coveted in Jquery that I propose

    const ID = "#input_fecha" //cambiarlo por el ID de tu input tag
    $(document).ready(function(){
    
       //Declaración de variables
       var fechaActual;
       var input_fecha;
      
      //init fechaActual a null
      fechaActual = null;
      //Obteniendo el input del dom
      input_fecha = $(ID);
      fechaActual = obtenerFechaActual();
      input_fecha.val(fechaActual); //asignando fecha actual al input text
    });
    
    
    //Devuelve todo los datos relacionada con la fecha
    //ejemplo Sun Apr 01 2018 20:49:05 GMT+0200 (Hora de verano romance)
    function obtenerFechaActual(){
    	return new Date();
    }
        
    answered by 01.04.2018 в 21:03