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

Question:

I have a code snippet that I can get the date and time, but the date is in the Mês/Dia/Ano format followed by the time, but I need the Dia/Mês/Ano followed by the time format, I've tried to change the form but it was incorrect, see 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 a timestamp .

Answer:

If what you initially have is a 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 also want to use hours, minutes and seconds you can use it like this:

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: https://jsfiddle.net/xt53v7hj/

Scroll to Top