简体   繁体   中英

Converting unix timestamp into time

So I want to convert unix timestamp into date time object, I am trying to do this with:

 async function getTime(unix){
  var date = new Date(unix * 1000);
  var hours = date.getHours();
  var mins = "0" + date.getMinutes();
  var secs = "0" + date.getSeconds();
  var formattedTime = hours + ':' + mins.substr(-2) + ':' + secs.substr(-2);

}

but I keep getting

Promise { undefined } 

on the table. can someone please tell me how to fix this?

So far as I can see your function, you don't return anything. You have to return the variable that you want. Also, there are no async calls within this function so it seems useless to make this an async function.

To add to this, the variable you want to return is probably formattedTime.

function getTime(unix) {
  const date = new Date(unix * 1000);
  const hours = date.getHours();
  const mins = "0" + date.getMinutes();
  const secs = "0" + date.getSeconds();
  return hours + ':' + mins.substr(-2) + ':' + secs.substr(-2);
}

Please note: I am using const, may that not be possible to use, use var instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM