JS is a release time for the UNIX timestamp? [Project measurement]

The time data given by the background interface is a UNIX timestamp. Our needs are to show that the WeChat circle shows that the release time is long before the current time, before “XX minutes”, “XX hours before”, “XX month ago “.

JS根据Unix时间戳显示发布时间是多久前【项目实测】

Conversion function:

 / ** * Unix timestamp converted to the current time How long before * @Param Timespan Int Unix timestamp * @Return Timespanstr String String * / Function Ftime (Timespan) {var DateTime = New Date (TimeSpan * 1000); VAR Year = datetime.getfullyear (); var month = datetime.getMonth () + 1; var day = datetime.getdate (); var hour = datetime.getHours (); var minute = datetime.getminutes (); // Current time Var Now = Date.Parse (New Date ()); // TypeScript Conversion Writing Var MilliseConds = 0; VAR Timespanstr; // Computation Time Difference MilliseConds = (NOW / 1000) - TimeSpan; // One minute IF (MilliseConds  <= 60) {
    timeSpanStr = '刚刚';
  }
  //大于一分钟小于一小时
  else if (60 < milliseconds && milliseconds <= 60 * 60) {
    timeSpanStr = Math.ceil((milliseconds / (60))) + '分钟前';
  }
  //大于一小时小于等于一天
  else if (60 * 60 < milliseconds && milliseconds <= 60 * 60 * 24) {
    timeSpanStr = Math.ceil(milliseconds / (60 * 60)) + '小时前';
  }
  //大于一天小于等于15天
  else if (60 * 60 * 24 < milliseconds && milliseconds <= 60 * 60 * 24 * 30) {
    timeSpanStr = Math.ceil(milliseconds / (60 * 60 * 24)) + '天前';
  }
  //大于一个月小于一年
  else if (60 * 60 * 24 * 30 < milliseconds && milliseconds <= 60 * 60 * 24 * 30 * 12){
    timeSpanStr = Math.ceil(milliseconds / (60 * 60 * 24 * 30)) + '个月前';
  }
  //超过一年显示
  else {
    timeSpanStr = year + '年' + month + '月' + day + '日 ' + hour + ':' + minute;  
  }
  return timeSpanStr;
} 
Summary

The above is the JS introduced by Xiaobian, according to UNIX timestamp display release time isHow long ago, I hope to help everyone. If you have any questions, please leave a message, Xiaobian will reply to everyone in time. Thank you very much for your support of Tumi Cloud Website!

If you think this article helps you, welcome to reprint, please indicate the source, thank you!


© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment