How to get Date in Day, Month, Year and Time format with Jquery

Asked

Viewed 21,755 times

5

I have a piece of code that I can get the date and time, but the date is in the format Mês/Dia/Ano followed by time, but precise in format Dia/Mês/Ano followed by the time, I tried to change the shape but it was incorrect, look 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.

  • 1

    It’s not just changing (d.getMont()+1).padLeft() with d.getDate().padLeft(), nay?

  • http://answall.com/a/22369/129

  • Hello @Felipe Avelar, I did this, but then appears to me the following: 04-2015 10:03:21, the day does not appear.

  • @adventistapr, man I switched here the two and worked normal. Try to put [d.getDate().padLeft(), (d.getMont()+1).padLeft(),... in the first vector and see if it solves your problem...

2 answers

9


If what you have initially 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 want to use also hours, minutes and seconds you can use 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/

2

Managed by solving as follows, 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 looked like this:

08/04/2015 10:53:15
  • 1

    But so the format was Dia/Mês/Ano HH:MM:SS and not Dia/Mês/Ano as you asked in the question...

  • 1

    Hello @Sergio, truth, I made an error in the formulation of the question, I commented that I needed in the format Day/Month/Year along with the date.

Browser other questions tagged

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