0

I am trying to conver unix timestamp to a readable time. I have tried all solutions I can find, none still work. I do not want to use any modules or frameworks. Id appreciate some help converting.(Angular) Please dont link to other questions. ive read them all. Thnk you

  var unix = Math.round(+new Date()/1000);
  console.log(unix) //works


  function secondstotime(unix)
{
    var t = new Date(1970,0,1);
    t.setSeconds(unix);
    var s = t.toTimeString().substr(0,8);
    if(unix > 86399)
      s = Math.floor((t - Date.parse("1/1/70")) / 3600000) + s.substr(2);
    console.log(s);
} 
secondstotime();
K Spring
  • 5
  • 1

2 Answers2

2

In angularjs you can simply use date filter to convert Unix timestamp (seconds). You just need to multiply Unix timestamp by 1000 to make it milliseconds timestamp. Here is the code

<p>{{1469424998 * 1000 | date:'hh:mm:ss'}}</p>

or you can use some external library like mommentjs to convert Unix Timestamp (seconds) to datetime like this

 var day = moment.unix(1469424998);
console.log(day.format("hh:mm:ss"));

I hope it will help

R.K.Saini
  • 2,678
  • 1
  • 18
  • 25
1

You question is not clear. In your current code, it prints following output:

1469424998
Invalid

It prints Invalid because you are missing parameter of secondstotime() method. If you add paameters to secondstotime() method call, it will print following output:

DEMO

var unix = Math.round(+new Date()/1000);
  console.log(unix) //works


  function secondstotime(unix)
  {
    var t = new Date(1970,0,1);
    t.setSeconds(unix);
    var s = t.toTimeString().substr(0,8);
    if(unix > 86399)
      s = Math.floor((t - Date.parse("1/1/70")) / 3600000) + s.substr(2);
    console.log(s);
 } 
 secondstotime(unix);

Output:

1469424875
56408173:34:35 

It is not clear what you exactly want. Can you show us your expected output?

Khalid Hussain
  • 1,675
  • 17
  • 25