如何计算两个小时之间的差异小时数?

时间:2018-04-21 05:41:50

标签: javascript

由于我是javascript的新手,所以我问这个问题如何找出两个小时之间的差异小时。我成功计算出差异但是当我以AM / PM的格式给出时间它给我输出为NAN请检查我的代码我在下面发帖并提前感谢:



function calculateTime() {
  var d2 = new Date('2018-02-12 03:00:00 AM');
  var d1 = new Date('2018-02-10 08:00:00 AM');
  var seconds =  (d2- d1)/1000;
  var hours = seconds/3600;
  console.log(hours);
}
calculateTime();




2 个答案:

答案 0 :(得分:1)

只需从彼此中减去日期对象。

IASTSimpleDeclaration

减法返回两个日期之间的差异(以毫秒为单位)。 36e5是60 * 60 * 1000的缩写,因此除以36e5将为您提供时间。

答案 1 :(得分:1)

Firefox将返回格式System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. ---> System.NullReferenceException: Object reference not set to an instance of an object. at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext) at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext) at System.Web.HttpApplication.System.Web.Util.ISyncContext.Enter() at System.Web.Util.SynchronizationHelper.SafeWrapCallback(Action action) at System.Threading.Tasks.Task.Execute() --- End of inner exception stack trace --- ---> (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object. at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext) at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext) at System.Web.HttpApplication.System.Web.Util.ISyncContext.Enter() at System.Web.Util.SynchronizationHelper.SafeWrapCallback(Action action) at System.Threading.Tasks.Task.Execute()<---

的无效日期

一种选择是在YYYY-MM-DD HH:MM:SS AM

分别使用年,月,日
  

新日期(年,月[,日[,小时[,分钟[,秒] [,   毫秒]]]]]);

像:

new Date()

Doc:new Date