时差,并在javascript中转换成小时和分钟 [英] Time difference and convert into hours and minutes in javascript

查看:83
本文介绍了时差,并在javascript中转换成小时和分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有时间值如下
开始时间如:09:00:00,
endTime like:10:00:00;
这里不需要日期值。
,所以这个值需要计算差异,并转换成小时和分钟,秒。



我尝试过:

  var test = new Date()。getTime(startTime); 
var test1 = new Date()。getTime(endTime);
var total = test1 - test;

有一段时间得到 NaN 1111111 某些数位格式。



如何转换成HH:MM:SS或任何其他方式来查找时差

解决方案

您可以对时间值进行更改:

  var diff = test1.getTime() -  test.getTime(); //这是一个以毫秒为单位的时间
var diff_as_date = new Date(diff);
diff_as_date.getHours(); // hours
diff_as_date.getMinutes(); // minutes
diff_as_date.getSeconds(); //秒


I am having the time values as follows starttime like : 09:00:00 , endTime like : 10:00:00 ; here no date value is needed. so this values need to calculate difference and convert into hours and minutes,seconds.

I had tried with :

var test = new Date().getTime(startTime); 
var test1 = new Date().getTime(endTime);
var total = test1 - test;

Some time am getting NaN and 1111111 some digit format.

How can I convert into HH:MM:SS, or any other way to find time difference.

解决方案

You can take a difference of the time values:

var diff = test1.getTime() - test.getTime(); // this is a time in milliseconds
var diff_as_date = new Date(diff);
diff_as_date.getHours(); // hours
diff_as_date.getMinutes(); // minutes
diff_as_date.getSeconds(); // seconds

这篇关于时差,并在javascript中转换成小时和分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆