Click the button and show only date and time

Asked

Viewed 55 times

0

I have this button to start the task:

$('#IniciarTarefa').on('click', function () {
    $(this).closest('.form-group').data('inicio', new Date().getTime());
});

$('#mostrar').on('click', function () {
    var time = $('.form-group').data('inicio');
    alert(new Date(time));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
    <label class="col-md-4 control-label" for="IniciarTarefa" id="acao"></label>
    <div class="col-md-4">
        <button id="IniciarTarefa" name="IniciarTarefa" class="btn btn-info">Iniciar Tarefa</button>
    </div>
</div>
<button id="mostrar">Mostrar quando tarefa foi iniciada</button>

But it shows the date this way:

inserir a descrição da imagem aqui

And this way I can not enter in the database. I wanted to show only the date in this way:

2019-03-22 17:14:45

2 answers

1


Using function demonstrated in another answer in English (source here)

 function formatDate(date) {
  var monthNames = [
"Janeiro", "Fevereiro", "Março",
"Abril", "Maio", "Junho", "Julho",
"Agosto", "Setembro", "Outubro",
"Novembro", "Dezembro"
  ];

  var day = date.getDate();
  var monthIndex = date.getMonth();
  var year = date.getFullYear();
  var hours = date.getHours();
  var min = date.getMinutes();
  var sec = date.getSeconds();

  return year + '-' + (monthIndex+1) + '-' + day + ' ' + hours + ':' + min + ':' + sec;
}


var dtTime;
$('#IniciarTarefa').on('click', function () {
   $(this).closest('.form-group').data('inicio', formatDate( new Date() ) );
});

$('#mostrar').on('click', function () {
    var time = $('.form-group').data('inicio');
    alert('Data: '+ time );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
    <label class="col-md-4 control-label" for="IniciarTarefa" id="acao"></label>
    <div class="col-md-4">
        <button id="IniciarTarefa" name="IniciarTarefa" class="btn btn-info">Iniciar Tarefa</button>
    </div>
</div>
<button id="mostrar">Mostrar quando tarefa foi iniciada</button>

  • but intended it to save button value beyond date also time

  • Ok, added. For more options, refer to Date class documentation: https://javascript.info/date

  • but the way it is only when it shows is that the date is formatted, but the show button and the function of the click The idea is to store in the value of the button with id="IniciarTarefa" the date and time formatted to then insert into the database table.

  • Dude, you already have the knife and the cheese in hand, you can manipulate the code however you want, anyway, adjusted script.

0

Good afternoon. This may help:

$('.jsData').on('click',()=>{
    obtainedData();
})


function obtainedData(){
  var fecha = new Date();
var options = { year: 'numeric', month: 'numeric', day: 'numeric',hour: 'numeric', minute: 'numeric', second:'numeric' };

alert(
  fecha.toLocaleDateString("pt-BR", options)
);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="jsData">Obtained Date</button>

Browser other questions tagged

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