我懒得在每个月底的时候填写我的工作时间表,所以我开始在PDF表单中添加一些功能。 Acrobat Pro提供使用JavaScript进行高级计算,但我遇到了这个问题。
我有两个字段,我在其中输入开始/结束工作的时间。我想计算我的加班时间并在第三个字段中输出结果。但是,我希望输出为十进制,所以当我加班半小时后,结果将是0.5
示例:我的工作时间是8.5小时,我从7.30开始到16.00(下午4点)结束。
到目前为止我的代码:
var workTime = this.getField ("Work time").value;
var startTime = this.getField ("Start time").value;
var endTime = this.getField ("End time").value;
event.value = workTime - (endTime - startTime);
答案 0 :(得分:17)
分开小时和分钟,将分钟数除以60
,再加上小时。
function timeStringToFloat(time) {
var hoursMinutes = time.split(/[.:]/);
var hours = parseInt(hoursMinutes[0], 10);
var minutes = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
return hours + minutes / 60;
}
答案 1 :(得分:1)
在我的情况下,我将其用于计算发票上的时间。
输入可以包含以下6种方式为用户编写它:
所以我用过这个(感谢Amadan),这是工作代码:
function time2dec(tIn) {
if(tIn == '')
return 0;
if(tIn.indexOf('h') >= 0 || tIn.indexOf(':') >= 0)
return hm2dec(tIn.split(/[h:]/));
if(tIn.indexOf('m') >= 0)
return hm2dec([0,tIn.replace('m','')]);
if(tIn.indexOf(',') >= 0)
return parseFloat(tIn.split(',').join('.')).toFixed(2);
if(tIn.indexOf('.') >= 0)
return parseFloat(tIn);
return parseInt(tIn, 10);
}
function hm2dec(hoursMinutes) {
var hours = parseInt(hoursMinutes[0], 10);
var minutes = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
return (hours + minutes / 60).toFixed(2);
}
使用示例(使用jQuery):
var qty = time2dec($('#qty').val());
var price = parseFloat($('#price').val());
var total = (qty * price).toFixed(2);
希望它可以帮助我们中的一些人。
答案 2 :(得分:0)
作为替代方案,我很好奇是否可以使用Date.parse
来完成,而且可以。
以下代码返回与所选答案相同的值,但垃圾输入除外,在这种情况下它返回零。使用1970日期是因为UNIX时间为零。
请注意,MDN说,"建议不要使用Date.parse直到ES5,解析字符串完全取决于实现。"
var test = [undefined, null, "", "garbage", "0", "00:00", " 01:11", "3:44", "2:3", "5.06"];
var hours = 0;
var html = "<table>";
for (var i = 0; i < test.length; i++) {
html += "<tr>";
html += "<td>" + test[i] + "</td>";
hours = timeStringToFloat(test[i]);
html += "<td>" + hours + "</td>";
hours = timeStringToNumber(test[i]);
html += "<td>" + hours + "</td>";
html += "<td>" + hoursToString(hours) + "</td>";
html += "</tr>";
}
stdout.innerHTML = html;
function timeStringToNumber(time) {
return ((new Date(("01 Jan 1970 " + time || "").replace(".", ":") + " GMT")) / 3600000) || 0;
}
function timeStringToFloat(time) {
try {
var hoursMinutes = time.split(/[.:]/);
var hours = parseInt(hoursMinutes[0], 10);
var minutes = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
return hours + minutes / 60;
} catch (e) {
return "ERROR";
}
}
function hoursToString(hours) {
var minutes = hours * 60;
return (
("00" + Math.floor(minutes / 60)).slice(-2) +
":" +
("00" + Math.round(minutes % 60)).slice(-2)
);
}
&#13;
body {
font-family: sans-serif;
font-size: 12px;
}
h4 {
color: white;
background-color: steelblue;
padding: 0.5em;
}
table {
border-collapse: collapse;
}
table td {
border: 1px solid gray;
min-height: 1em;
}
&#13;
<h4>Test Output:</h4>
Method A is the original and Method B the alternative.
<table>
<thead>
<tr>
<th>String</th>
<th>Method A</th>
<th>Method B</th>
<th>ToString</th>
</tr>
</thead>
<tbody id="stdout"></tbody>
</table>
&#13;
*
function timeStringToNumber( time )
{
return ((new Date(("01 Jan 1970 " + time || "").replace(".",":") + " GMT")) / 3600000) || 0;
}