Convert timestamp value to date

3

I have a timestamp value of a date, 1389135600 , how can I convert this value to a date of this type 27/12/2014 .

    
asked by anonymous 27.12.2014 / 12:38

1 answer

4

Assuming this number to be a valid timestamp would just need to create a date based on it. As we should only consider the seconds, it is multiplied by 1000.

With the date in hand you can use it in the format you want. The data type can do this with its own methods for some common formats, such as toLocaleDateString .

The meuLog function was used just to make printing easier.

var date = new Date(1389135600*1000); // converte para data

meuLog(date.toLocaleDateString("pt-BR")); //formata de acordo com o requisito

function meuLog(msg) {
  div = document.body;
  div.innerHTML = div.innerHTML + '<p>' + msg + '</p>';
}
    
27.12.2014 / 12:50