Convert time to String - Node.js String

Node.js examples for String:Convert

Description

Convert time to String

Demo Code

function time2String(time){
  var date = new Date(time);
  var now = new Date();

  var dif = (now - date) / 1000;

  if(dif < 300){
    return "moment ago";
  }//w  ww . ja v  a 2s.c  om
  else if(dif < 3600){
    return (dif / 60).toFixed(0) + " mins ago";
  }
  else if(dif < 7200){
    return "a hour ago";
  }
  else if(dif < 86400){
    return (dif / 3600).toFixed(0) + " hours ago"; 
  }
  else if(dif < 172800){
    return "yesterday";
  }
  else{
    return date.toDateString();
  }


  return date.getFullYear() + "-" + date.getMonth() + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes();
}

Related Tutorials