Home > Web Front-end > JS Tutorial > body text

JS calculation of time difference from the current time technology sharing

小云云
Release: 2017-12-29 14:07:20
Original
1580 people have browsed it

This article mainly shares an example of JS calculating the time difference from the current time. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.

As shown below:

/**
 * JS获取距当前时间差
 * 
 * @param int time 时间戳格式
 *
 */
function get_time_diff(time) {
 var diff = '';
 var time_diff = new Date().getTime() - time; //时间差的毫秒数 
 
 //计算出相差天数 
 var days = Math.floor(time_diff / (24 * 3600 * 1000));
 if (days > 0) {
 diff += days + '天';
 }
 //计算出小时数 
 var leave1 = time_diff % ( 24 * 3600 * 1000); 
 var hours = Math.floor(leave1 / (3600 * 1000));
 if (hours > 0) {
 diff += hours + '小时';
 } else {
 if (diff !== '') {
  diff += hours + '小时';
 }
 }
 //计算相差分钟数 
 var leave2 =leave1 % (3600 * 1000);
 var minutes = Math.floor(leave2 / (60 * 1000));
 if (minutes > 0) {
 diff += minutes + '分';
 } else {
 if (diff !== '') {
  diff += minutes + '分';
 }
 }
 //计算相差秒数 
 var leave3 = leave2%(60*1000);
 var seconds = Math.round(leave3/1000);
 if (seconds > 0) {
 diff += seconds + '秒';
 } else {
 if (diff !== '') {
  diff += seconds + '秒';
 }
 }
 
 return diff;
}
Copy after login

Related recommendations:

Detailed explanation of the method of calculating time difference in PHP

Sharing on how to implement time comparison and time difference calculation in PHP

js How to find the time difference example code

The above is the detailed content of JS calculation of time difference from the current time technology sharing. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template