Convert timestamp value to date

Posted on

Question :

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


Answer :

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>';


Leave a Reply

Your email address will not be published. Required fields are marked *