JS 在数小时内获得 GMT 的两次不同问题

JS Get Two Time Different problem with GMT in hours

我遇到了一个问题,要在全球范围内获得两次不同的时间。我需要 diff 时间格式 days: Hours: Minutes: Seconds. 一切正常,但在 小时 后出现问题。对于此示例,小时应该是 0 小时,但它是 return 6 小时。

function getTimeDiff(join, lastSeen) {

  let t1 = new Date(join).getTime(),
    t2 = new Date(lastSeen).getTime(),
    milliseconds = 0,
    time = '';
  if (isNaN(t1) || isNaN(t2)) return '';
  if (t1 < t2) {milliseconds = t2 - t1;}
  else {milliseconds = t1 - t2;}
  var days = Math.floor(milliseconds / 1000 / 60 / (60 * 24));
  var date_diff = new Date(milliseconds);
  if (days > 0) time += days + 'd ';
  console.log('join: ', join, ' - ', 'lastseen:', lastSeen);
  console.log('Diff = ', milliseconds);
  console.log('Standard Time : ', date_diff);
  console.log('Hours: ', date_diff.getHours());
  console.log('Minutes: ', date_diff.getMinutes());
  console.log('Secounds: ', date_diff.getSeconds());
  if (date_diff.getHours() > 0) time += date_diff.getHours() + 'h ';
  if (date_diff.getMinutes() > 0) time += date_diff.getMinutes() + 'm ';
  if (date_diff.getSeconds() > 0) time += date_diff.getSeconds() + 's ';
  console.log('Result: ', time);
  return time;

}

console.log(getTimeDiff(1589911275699, 1589911365116));

输出结果:

**

1589911275699 " -- " 1589911365116
join:  1589911275699  -  lastseen: 1589911365116
Diff =  89417
Standard Time :  Thu Jan 01 1970 06:01:29 GMT+0600 (Bangladesh Standard Time)
Hours:  6 // Result should be 0
Minutes:  1
Secounds:  29
Result:  6h 1m 29s // 0h 1m 29s

**

这里已经有一些 post 但同样的问题

当您构造日期对象 (new Date(milliseconds)) 时,它会在您的时区中创建日期对象(我认为您的格林威治标准时间为 +6),因此您可以多获得 +6 小时。

简单的解决方法是应用时区偏移量:

var hours = date_diff.getHours() + new Date().getTimezoneOffset()/60; if (hours > 0) time += hours + 'h ';

但是这不会涵盖具有半小时偏移的时区 https://en.wikipedia.org/wiki/Time_zone#/media/File:Solar_time_vs_standard_time.png 例如澳大利亚有 +9:30 offset timezone

最好手动获取时差:

var milliseconds = 89417;
var hours = Math.floor(milliseconds/1000/60/60);
var minutes = Math.floor((milliseconds - hours*1000*60*60)/1000/60);
var seconds = Math.floor((milliseconds - hours*1000*60*60 - minutes*1000*60)/1000);

console.log(hours, minutes, seconds);

PS:

我建议使用 moment.js 来计算时差, 检查这个

你不需要转换成Date对象,你有时间值就足够了。例如

function getTimeDiff(t0, t1) {
  let z = n => (n<10?'0':'')+n;
  let diff = t1 - t0;
  let sign = diff < 0? '-':'+';
  diff = Math.abs(diff);
  let h = diff / 3.6e6 | 0;
  let m = (diff % 3.6e6) / 6e4 | 0;
  let s = (diff % 6e4) / 1e3 | 0;
  
  return `${sign}${h}:${z(m)}:${z(s)}`; 
}

let join = 1589911275699;
let lastseen = 1589911365116;
console.log(`The time difference between 
${new Date(join)} and
${new Date(lastseen)} is
${getTimeDiff(join, lastseen)}`);

请注意,生成的时间戳中的差异不同于时间值中的差异,因为转换为时间戳会截断毫秒(十进制秒)。

转换为 Date 对象没有任何用处,因为:

new Date(join).getTime() === join

其中 join 是 ±8.64e15 范围内的数字。这给出了 1970 年 1 月 1 日两侧各 1 亿天的范围。

这是@RobG

修改后的代码片段

 function getTimeDiff(t0, t1) {
        var s,m,h,d,tDiff="";
        let z = n => (10 > n?'0':'')+n;
        let diff = t1 - t0;
        if(diff < 0) {return "0s"};
        diff = Math.abs(diff);
        d = Math.floor(diff / 86.4e6 | 0);
        h = Math.floor(diff / 3.6e6 | 0);
        if(d > 0){
            tDiff += d + "d ";
            h -= (d * 24);
        }
        m = Math.floor((diff % 3.6e6) / 6e4 | 0);
        s = Math.floor((diff % 6e4) / 1e3 | 0);
        if(h > 0) {tDiff += h + "h ";}
        if(m > 0) {tDiff += m + "m ";}
        tDiff += s + "s";
        return tDiff;
    }

let join = 1589645159314;
//let join = new Date(Date.now() - (1 * 22 * 60 * 60 * 1000)).getTime();
//let lastseen = 1589911365116;
let lastseen = Date.now();
console.log(`The time difference between 
${new Date(join)} and
${new Date(lastseen)} is
${getTimeDiff(join, lastseen)}`);

function getTimeDiff(t0, t1) {
  let z = n => (n<10?'0':'')+n;
  let diff = t1 - t0;
  let sign = diff < 0? '-':'+';
  diff = Math.abs(diff);
  let h = diff / 3.6e6 | 0;
  let m = (diff % 3.6e6) / 6e4 | 0;
  let s = (diff % 6e4) / 1e3 | 0;
  
  return `${sign}${h}:${z(m)}:${z(s)}`; 
}

let join = 1589911275699;
let lastseen = 1589911365116;
console.log(`The time difference between 
${new Date(join)} and
${new Date(lastseen)} is
${getTimeDiff(join, lastseen)}`);