0
votes

I am missing some sort of conversion here or something when using google apps script to find the difference between times. I am creating a timesheet to be used by school staff in google sheets. Using baby steps, I am trying to find the number of hours/minutes the employee entered on the first Sunday entry; 6:00AM - 9:00AM, on 6/14/2020. My code is giving me 10,800,000; and the dates in the log are not matching what the spreadsheet shows, which tells me it needs to know more? Once I can understand this, I can move on. Thank you.

This is the code I'm using:

function onEdit() {
  // get the current sheet
  var ss = SpreadsheetApp.getActiveSheet();  

  // set variables to be used for Sunday, Week 1
  var Week1Sun1_In = ss.getRange('B7').getValue();
  var Week1Sun1_Out = ss.getRange('B8').getValue();
  var Week1Sun1_Hours = ss.getRange('B9').setValue(Week1Sun1_Out - Week1Sun1_In);

  Logger.log(Week1Sun1_In,Week1Sun1_Out,Week1Sun1_Hours);    
}

And this is what I see in the log:

LOGS: [20-06-19 11:43:59:581 PDT] Sat Dec 30 06:00:00 GMT-05:00 1899 Sat Dec 30 09:00:00 GMT-05:00 1899 Range

And this is a snippet from the spreadsheet that will show what I'm trying to do: enter image description here

1

1 Answers

0
votes
function timeDiffHours(Start,End) {
  if(Start && End) {
    var second=1000;
    var minute=60*second;
    var hour=minute*60;
    var day=hour*24;
    var t1=new Date(Start).valueOf();
    var t2=new Date(End).valueOf();
    var d=t2-t1;
    var days=Math.floor(d/day);
    var hours=Math.floor(d%day/hour);
    var minutes=Math.floor(d%day%hour/minute);
    var seconds=Math.floor(d%day%hour%minute/second);
    return 'dd:hh:mm:ss\n' + days + ':' + hours + ':' + minutes + ':' + seconds;  
  } else {
    return 'Invalid Inputs';
  }
}