我正在尝试找出一种方法来计算同一字段中两个数字(24小时制)之间的差异
{
xtype: "textfield",
fieldLabel: "Monday",
name: "mon1"
}, {
xtype: "numberfield",
fieldLabel: "Hours",
name: "hoursMon1",
}
基本上,用户输入看起来像这样:
mon1 = 7.15-16.45
然后,我需要一个函数来计算hoursMon1
字段中的差异(即“ 9.30”)。
由于程序的工作原理,我不能仅将输入分成2个字段并比较差异,所以有什么办法可以使我仅从1个字段执行类似的操作?
答案 0 :(得分:0)
在console.log函数中获取时差
var time = "7.15-16.45".split("-");
console.log(msToTime(getMillsec(time[1])- getMillsec(time[0]))) //output 09-30
//get milliseconds
function getMillsec(ctime){
var timeParts = ctime.split(".");
return ((+timeParts[0] * (60000 * 60)) + (+timeParts[1] * 60000));
}
//convert millisecond to hour:min format
function msToTime(duration) {
var milliseconds = parseInt((duration%1000)/100)
, seconds = parseInt((duration/1000)%60)
, minutes = parseInt((duration/(1000*60))%60)
, hours = parseInt((duration/(1000*60*60))%24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
return hours + "-" + minutes;
}