How to get Date in the format day, month, year and time with JQuery

5

I have a code snippet that I can get the date and time, but the date is in the format Mês/Dia/Ano followed by the time, but I need the format Dia/Mês/Ano followed by the time, I already tried to change the form but it was incorrect, What I have:

Number.prototype.padLeft = function(base,chr){
    var  len = (String(base || 10).length - String(this).length)+1;
    return len > 0? new Array(len).join(chr || '0')+this : this;
}

// Exibindo data no input ao iniciar tarefa
var d = new Date,
    dformat = [ (d.getMonth()+1).padLeft(),
                d.getDate().padLeft(),
                d.getFullYear()
              ].join('-') +
              ' ' +
              [ d.getHours().padLeft(),
                d.getMinutes().padLeft(),
                d.getSeconds().padLeft()
              ].join(':');

Note: The initial format is timestamp .

    
asked by anonymous 08.04.2015 / 14:57

2 answers

8

If what you initially have is timestamp you can convert using this function:

function dataFormatada(d) {
    var data = new Date(d),
        dia  = data.getDate(),
        mes  = data.getMonth() + 1,
        ano  = data.getFullYear();
    return [dia, mes, ano].join('/');
}

Example:

function dataFormatada(d) {
  var data = new Date(d),
    dia = data.getDate(),
    mes = data.getMonth() + 1,
    ano = data.getFullYear();
  return [dia, mes, ano].join('/');
}

alert(dataFormatada(1382086394000));

If you want to use hours, minutes and seconds too, you can use this way:

function dataFormatada(d) {
    var data = new Date(d),
        dia = data.getDate(),
        mes = data.getMonth() + 1,
        ano = data.getFullYear(),
        hora = data.getHours(),
        minutos = data.getMinutes(),
        segundos = data.getSeconds();
    return [dia, mes, ano].join('/') + ' ' + [hora, minutos, segundos].join(':');
}

jsFiddle: link

    
08.04.2015 / 15:11
1

I have solved this by following a suggestion,

// Exibindo data no input ao iniciar tarefa
var d = new Date();
dataHora = (d.toLocaleString());    
// alert(d.toLocaleString());

// Mostrando data no campo
$('#DataInicio').val();
$('#DataInicio').val(dataHora);

The date displayed in my input looks like this:

08/04/2015 10:53:15
    
08.04.2015 / 15:56