0

I'm working on a Google script where it's trying to check if a given time A is between time B and time C: IE:

var A = getCurrentSheet.getRange("P30").getDisplayValue();
//08:40:00 AM
var B = getCurrentSheet.getRange("P31").getDisplayValue();
//04:30:00 AM
var C = getCurrentSheet.getRange("P32").getDisplayValue();
//10:40:00 AM

The goal is to create a logical comparison where:

if(B <= A <= C)

Though I'm having some difficulty figuring out how to get the script to recognize these values as time in terms of HH:mm:ss

Tiamat616
  • 1
  • 2

1 Answers1

0
function compareTimes() {
  const sh = SpreadsheetApp.getActiveSheet();
 const [b,a,c] = sh.getRange( 2, 1, 1, 3).getValues().flat().map(e => new Date(e).valueOf());//using Date constructor to get date object and then valueOf() methold to get milliseconds from reference data
 if(b <= a <= c) SpreadsheetApp.getUi().alert('It is true.');
}

active sheet:

b a c
9/26/2021 01:30:00 9/26/2021 02:30:00 9/26/2021 03:30:00

Dialog:

enter image description here

Cooper
  • 59,616
  • 6
  • 23
  • 54