te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>angular - How to calculate difference between two timestamps using javascript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angular - How to calculate difference between two timestamps using javascript - Stack Overflow

programmeradmin3浏览0评论

I have two timestamps and I need difference between them in hours. How to calculate them.

var resolution
var EndTime = 1541092163000
var StartTime = 1541077763000
resolution = slaEndTime - slaStartTime
var resolutionTime = ((resolution / (1000 * 60)) % 60)

I have two timestamps and I need difference between them in hours. How to calculate them.

var resolution
var EndTime = 1541092163000
var StartTime = 1541077763000
resolution = slaEndTime - slaStartTime
var resolutionTime = ((resolution / (1000 * 60)) % 60)

Share Improve this question edited Nov 22, 2018 at 10:58 arun kumar asked Nov 21, 2018 at 13:24 arun kumararun kumar 991 gold badge1 silver badge6 bronze badges 5
  • I'm assuming the sla prefix is an issue with copying your code to SO? – George Commented Nov 21, 2018 at 13:26
  • 1 Possible duplicate of How can I calculate the difference between two times that are in 24 hour format? – Gerard Commented Nov 21, 2018 at 13:27
  • 2 That's not a duplicate... the time here is not in 24 hour format – Oram Commented Nov 21, 2018 at 13:28
  • First convert both to same time format, may be to IST, then convert to milliseconds and find the difference – Raja Mohamed Commented Nov 21, 2018 at 13:30
  • 1 Possible duplicate of Get difference between 2 dates in JavaScript? – adiga Commented Nov 21, 2018 at 13:37
Add a ment  | 

6 Answers 6

Reset to default 8

You just need to divide rather than do times and modulus

/1000 will convert it to seconds

the first /60 will convert to minutes

and the last /60 hours

var resolution
var EndTime = 1541092163000
var StartTime = 1541077763000
resolution = EndTime - StartTime
var resolutionTime = (((resolution / 1000) / 60)/ 60)
console.log(resolutionTime)

Or you can use momentjs https://momentjs./

See the snippet below. You need to divide by 1000 then by 60 and then by 60 again to get the hours. In short you need do divide by 1000*60*60=3600000.

var endTime = 1541092163000;
var startTime = 1541077763000;
var differenceInMiliseconds = endTime - startTime;
var differenceInSeconds = differenceInMiliseconds / 1000;
var differenceInMinutes = differenceInSeconds / 60;
var differenceInHours = differenceInMinutes / 60;
console.log(differenceInHours);

// or in short
console.log((endTime - startTime) / 3600000);

have you tried to do this ?

var resolution
var EndTime = 1541092163000
var StartTime = 1541077763000
var resolution = EndTime - StartTime
var resolutionTime = (parseFloat(resolution) / (60000*60) ) 
console.log(resolutionTime)

Save yourself from manual dates manipulation insanity and use moment.js

const moment = require('moment');

var EndTime = 1541092163000
var StartTime = 1541077763000
var resolution = moment(EndTime - StartTime).asHours();

This approach helped to get a basic hours, minutes, seconds breakdown.

const Duration = (difference) => {
    let secondsInMiliseconds    = 1000, 
        minutesInMiliseconds    = 60 * secondsInMiliseconds,
        hoursInMiliseconds      = 60 * minutesInMiliseconds;

    var differenceInHours       = difference / hoursInMiliseconds,
         differenceInMinutes    = differenceInHours     % 1 * 60,
         differenceInSeconds    = differenceInMinutes   % 1 * 60;
    return {
        "hours"   : Math.floor(differenceInHours),
        "minutes" : Math.floor(differenceInMinutes),
        "seconds" : Math.floor(differenceInSeconds)
    }
}
let aLittleWhileAgo = (new Date()-10000000)
let now = new Date();
console.log(Duration(now-aLittleWhileAgo))

Use ms to calculate the difference between two timestamps

const ms = require('ms');

var EndTime = 1541092163000;
var StartTime = 1541077763000;

ms( EndTime - StartTime) //return 4h

发布评论

评论列表(0)

  1. 暂无评论