Put variable inside input type="date"

Asked

Viewed 685 times

0

How can I put this variable inside input

var data = new Date();
    var dataCurrente = new Date();
    if (data > dataCurrente) {
        alert("Data maior que atual");
    }

In case the new date() has to be the date I put on input type="date".

  • is confused your question, "input" means in the value?

1 answer

2

I put some Alert's just so you can see how it goes riding the date until the final presentation.

var agora = new Date();
alert(agora);

var dia = ("0" + agora.getDate()).slice(-2);
alert("Hoje é dia: " + dia);

var mes = ("0" + (agora.getMonth() + 1)).slice(-2);
alert("Do mês: " + mes);

var dataHoje = agora.getFullYear()+"-"+(mes)+"-"+(dia);
alert("Data formatada para o campo: " + dataHoje);

$('#campoQueVoceQuerSetarValor').val(dataHoje);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="campoQueVoceQuerSetarValor" type="date"></input>

However much presentation in the field will be dd/MM/yyyy, note that the shape that goes there is yyyy/MM/dd.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.