How to convert timestamp to readable date/time?

22,139

Solution 1

You can convert this to a readable date using new Date() method

if you have a specific date stamp, you can get the corresponding date time format by the following method

var date = new Date(timeStamp);

in your case

var date = new Date(1447804800000);

this will return

Wed Nov 18 2015 05:30:00 GMT+0530 (India Standard Time)

Solution 2

Call This function and pass your date :

JS :

function getDateFormat(date) {
var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

if (month.length < 2)
    month = '0' + month;
if (day.length < 2)
    day = '0' + day;
var date = new Date();
date.toLocaleDateString();

return [day, month, year].join('-');
}
;

Solution 3

In my case, the REST API returned timestamp with decimal. Below code snippet example worked for me.

var ts= 1551246342.000100; // say this is the format for decimal timestamp.
var dt = new Date(ts * 1000);
alert(dt.toLocaleString()); // 2/27/2019, 12:45:42 AM this for displayed
Share:
22,139
Kemat Rochi
Author by

Kemat Rochi

Updated on July 09, 2022

Comments

  • Kemat Rochi
    Kemat Rochi almost 2 years

    I have an API result giving out timestamp like this 1447804800000. How do I convert this to a readable format using Javascript/jQuery?

    • Admin
      Admin about 8 years
      var x = new Date(1447804800000); Returns: Wed Nov 18 2015 11:00:00
    • Pramod Karandikar
      Pramod Karandikar about 8 years
    • Kunal Gadhia
      Kunal Gadhia about 8 years
      did you get the expected output?
    • Kemat Rochi
      Kemat Rochi about 8 years
      @KunalGadhia - Yeah now it works great. Thanks for sharing that function :)
    • Kemat Rochi
      Kemat Rochi about 8 years
      @KunalGadhia - Do you also know how to do it backwards? Like, given Wed Nov 18 2015 05:30:00 GMT+0530 (India Standard Time) and covert it to 1447804800000 ??
  • Kemat Rochi
    Kemat Rochi about 8 years
    Do you also know how to do it backwards? Like, given Wed Nov 18 2015 05:30:00 GMT+0530 (India Standard Time) and covert it to 1447804800000 ??
  • Nitheesh
    Nitheesh about 8 years
    Javascript does not provides any inbuilt method for reverse conversion of date to date time stamp. We may need to write a custom method for this purpose.