Fill in a text field and turn it upside down in the format yy/mm/dd hh:mm

Asked

Viewed 59 times

-1

Good morning guys, I need to find a way to fill in a field date and at the time of filling in it be inserted in another input, but in the American format, because I will use this field to make an Insert in a BD. I’ve started some kind of test,

var input = document.querySelector('input');
var div = document.querySelector('div');

input.addEventListener('keyup', function () {

    div.innerHTML = input.value;
});

I know that so I type and it already prints elsewhere, but how to join the way . split?

  • What is the input format and what should be the output format?

  • enters BR format and exits EN format for insert in the bank. EX. 29/08/2018 10:24 and exits 2018-08-29 10:24

  • 1

    dd/mm/yyyy hh:ii for yyyy/mm/dd hh:ii? And this value will be used in backend, you shouldn’t add it into a new input? The text within a div is not sent in the form submission.

  • dd/mm/yyyy hh:ii for yyyy/mm/dd hh:ii, that’s right, yes I’ll add in a new input. the example above is only to show the addeventlistener event

  • if you are using php as backend, then it is best to treat this date by php

  • Development is not in PHP.

  • my idea, would be to use a <input type="text" Hidden> to receive the date in the format I wish.

  • You can use the date type in the html input, which does the entire exchange process. Read here

Show 3 more comments

2 answers

1

I decided as follows:

In the <head> I used Javascript below:

<script type="text/javascript">
    var dialanform = document.getElementById("dia").value;
    var ditual = dialanform.split(" ");
    var y1 = ditual[0];
    var diamod = x1.split("/");
    var y3 = diamod[2] + '-' + diamod[1] + '-' + diamod[0];
    var diatime = datalanform.split(":");
    var y1time = ditual[1]


    document.querySelector('input[name="diaslice"]').value = y3 + " " + y1time;
</script>

And in HTML I selected the function onblur="preencher_d(this)".

With that the field input that needed reversed in the desired format.

1

You could do it in a simpler way with jquery, or a scripting function.

<html>
    <input type="date" name="data" id="data">
</html>

<script>
//exemplo com jquery
$('#data').change(function(){ //talvez seja melhor usar o change do que o blur, onde ele só irá salvar se houver uma alteração.
        var dataReal = $(this).val(); //por pdrão o input date já ve no formato amaricano.
        var partes = dataReal.split("-"); //separa os valores pelo '-' em um array

        //exemplo 1 - separando o array em variaveis
        var dia = partes[2];
        var mes = partes[1];
        var ano = partes[0];
        var dataBr = dia+"/"+mes+"/"+ano;

        //exemplo 2 - montagem direta
        var dataBr = partes[2]+"/"+partes[1]+"/"+partes[0];
        alert(dataBr);
    });
</script>

Browser other questions tagged

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