How to convert time to decimal in javascript?

I'm too lazy to fill out my work time sheet to the end at the end of each month, so I started adding some features to our PDF form. Acrobat Pro offers advanced computing using JavaScript, but I'm stuck with this problem.

I have two fields in which I enter the time when I start / finish work. I want to calculate my overtime work and display the result in the third field. however, I want the result to be decimal, so when I get overtime for half an hour, the result will be 0.5

Example: my working time is 8.5 hours, I start at 7.30 and end at 16.00 (4 pm).

My code is:

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); 
+4
source share
3 answers

Divide hours and minutes, divide minutes by 60 , add hours.

 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; } 
+13
source

In my case, I use it to calculate the time on the account .

The input may contain these 6 ways to write it to the user:

  • 1 → 1 hour 0 minutes
  • 1.2 → 1 hour 12 minutes
  • 1.5 → 1 hour 30 minutes
  • 1:30 → 1 hour 30 minutes
  • 1h40 → 1 hour 40 minutes
  • 45 m → 0 hour 45 minutes

So, I used this (thanks to Amadan), here is the 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); } 

Usage example (with jQuery):

 var qty = time2dec($('#qty').val()); var price = parseFloat($('#price').val()); var total = (qty * price).toFixed(2); 

Hope this helps some of us.

+1
source

Another way;)

 var aHours = 4.45; var aHoursInMinutes = (Math.floor(aHours) * 60 )+ ((aHours - Math.floor(aHours)) * 100); // 285 minutes; var afromMinutesToHours = (Math.floor(aHoursInMinutes /60) + ((aHoursInMinutes - (60* Math.floor(aHoursInMinutes /60)))/100)); // 4.45 hours; var bHours = 0.33; var bHoursInMinutes = (Math.floor(bHours) * 60 )+ ((bHours - Math.floor(bHours)) * 100); // 33 minutes; var bFromMinutesToHours = (Math.floor(bHoursInMinutes / 60) + (( bHoursInMinutes - (60 * Math.floor(bHoursInMinutes / 60)))/100)); // 0.33 hours; var resultInMinutes = aHoursInMinutes + bHoursInMinutes; var resultToHours = (Math.floor(resultInMinutes / 60) + (( resultInMinutes - (60 * Math.floor(resultInMinutes / 60)))/100)); // 5.18 hours; 
0
source

Source: https://habr.com/ru/post/1416104/


All Articles