I\'m too lazy to fill out my time sheet at work by the end at the end of every month, so I\'ve started adding some functions to our PDF form. Acrobat Pro offers to make advanced
In my case I use it for calculating time on invoice.
The input could contain these 6 ways to write it for the user :
So I used this (thanks to Amadan), here is working code :
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);
}
Example of use (with jQuery) :
var qty = time2dec($('#qty').val());
var price = parseFloat($('#price').val());
var total = (qty * price).toFixed(2);
Hope it could help some of us.