JavaScript(使用 C#)
我想找到两个时间分量之间的差异,但我没有得到我想要的!
我的表单中有 2 个下拉列表组件,
这是我的代码:
input set 1:
timefrm_ 4 -> 18:00
timeto_4 --> 23: 00
input set 2 :
timefrm_ 4 -> 23:00
timeto_4 --> 01 : 00
JavaScript 代码:
function time_diif4()
{
var timespan4from = HMStoSec1(timefrm_4);
var timespan4to = HMStoSec1(timeto_4);
var timespan_4 = timespan4to - timespan4from ;
var diff4 = convertMinutes(timespan_4);
alert(timespan4from);
alert(timespan4to);
alert(timespan_4);
alert(diff4);
}
var secondsPerMinute = 60;
var minutesPerHour = 60;
function HMStoSec1(T)
{
// h:m:s
var A = T.split(/\D+/) ;
return ((A[0]*60 + +A[1])*60 )
}
function convertMinutes(intSeconds)
{
return Math.floor(intSeconds/secondsPerMinute);
}
我的输出(作为警报)是:
对于输入集 1:
64800(以秒为单位)
82800(以秒为单位)
18000(以秒为单位)
300(分钟)--- 5 小时 ( 18:00 至 23:00)
对于输入集 2:
82800
5400
-77400(错误,应该是第二天)
-1290
I want to find difference between two time-components, but I am not getting what I want!
I have 2 drop-down list components in a form,
here is my code:
input set 1:
timefrm_ 4 -> 18:00
timeto_4 --> 23: 00
input set 2 :
timefrm_ 4 -> 23:00
timeto_4 --> 01 : 00
JavaScript code:
function time_diif4()
{
var timespan4from = HMStoSec1(timefrm_4);
var timespan4to = HMStoSec1(timeto_4);
var timespan_4 = timespan4to - timespan4from ;
var diff4 = convertMinutes(timespan_4);
alert(timespan4from);
alert(timespan4to);
alert(timespan_4);
alert(diff4);
}
var secondsPerMinute = 60;
var minutesPerHour = 60;
function HMStoSec1(T)
{
// h:m:s
var A = T.split(/\D+/) ;
return ((A[0]*60 + +A[1])*60 )
}
function convertMinutes(intSeconds)
{
return Math.floor(intSeconds/secondsPerMinute);
}
my output (as alert) is:
for input set 1:
64800 (in seconds)
82800 (in seconds)
18000 (in seconds)
300 (in minutes) --- 5 hours ( 18:00
to 23:00)
for input set 2:
82800
5400
-77400 ( error it should be next day)
-1290
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需找到除以一天分钟后的剩余值即可。
当开始时间大于结束时间时,则表示结束时间属于第二天。这段代码将解决这个问题。但如果您不希望发生这种情况,您应该阻止用户选择此数据。
just find the remaining value in division to a day minutes.
when the start time is greater than finish time then it means that finish time belongs to next day. this code will solver this problem. but if you don't want this to happen, you should prevent user to selecting this data.