简体   繁体   English

JavaScript / Extjs - 计算时间戳的小时数

[英]JavaScript / Extjs - Calculate number of hours from time stamp

I have this time stamp format for each car in my map: 我的地图中的每辆车都有这个时间戳格式:

2012-12-11T03:51:43+03:00

I want to extract the number of hours from it according to current time. 我想根据当前时间从中提取小时数。

I don't know how to parse this string then compare it to current time. 我不知道如何解析这个字符串然后将它与当前时间进行比较。

Any Idea ? 任何想法 ?

something like: 就像是:

var
  d1 = new Date('2012-12-11T03:51:43+03:00'),
  d2 = new Date;

console.log(
  (d2 - d1) / 3600000
);

You need to first fix the timestamp for other browsers than Chrome 您需要先修复Chrome以外的其他浏览器的时间戳

javascript date.parse difference in chrome and other browsers Chrome和其他浏览器中的javascript date.parse差异

DEMO DEMO

var noOffset = function(s) {
  var day= s.slice(0,-5).split(/\D/).map(function(itm){
    return parseInt(itm, 10) || 0;
  });
  day[1]-= 1;
  day= new Date(Date.UTC.apply(Date, day));  
  var offsetString = s.slice(-5)
  var offset = parseInt(offsetString,10)/100;
  if (offsetString.slice(0,1)=="+") offset*=-1;
  day.setHours(day.getHours()+offset);
  return day.getTime();
}
alert(parseInt((new Date().getTime()-noOffset(yourTimeStamp))/3600000))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM