Home > Back-end >  How to compare timeStamp whe i can't seem to bring them to a common format using GAS?
How to compare timeStamp whe i can't seem to bring them to a common format using GAS?

Time:02-19

So, on form submit, I got a timestamp, which looks like this: "values":["2/18/2022 14:11:25"]

I then need to compare it with the one on the spreadsheet, so that I can set a number to an adjacent column. Then, I'm using the code below, but I'm facing an error on ```Utilities.formatDate()````

The code:

function onSubmit(e) {
  Logger.log("%s", JSON.stringify(e));
  const timeStamp = e.values[0]
  const formRespSheet = e.source.getSheetByName('Form Responses 1')
  var maxNumber = Math.max.apply(null, formRespSheet.getRange(2, 14, formRespSheet.getLastRow(), 1).getValues());
  maxNumber = maxNumber   1

  Utilities.sleep(1000);//Tried it
  const allTimeStamps = formRespSheet.getRange(2, 1, formRespSheet.getLastRow(), 1).getValues();

  for (let a = 0; a < allTimeStamps.length; a  ) {
    let sheetTimeStamp = allTimeStamps[a]
    sheetTimeStamp = Utilities.formatDate(sheetTimeStamp, Session.getTimeZone(), "MM/dd/yyyy HH:mm:ss")
    if (sheetTimeStamp.valueOf() == timeStamp.valueOf()) {
      const row = a   1
      formRespSheet.getRange(row, 14).setValue(maxNumber)
    }
  }
}

The error says:

The parameters (number[],String,String) don't match the method signature for Utilities.formatDate.

Thanks for your help.

CodePudding user response:

Google Spreadsheet has a Date object that works very well for comparion. I always, and don't understand why everyone doesn't, set the number format of a cell or cells to Date. That way I can compare dates, subtract dates, etc. etc. Now, I know the time interers but a simple function can alleviate that problem, I have a spreadsheet that has two Dates that have the same date but they have different times. So I can compare if they are the same day.

function test() {
  try {
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
    var range = sheet.getRange("B1:B2");
    var values = range.getValues();
    function removeTime( value ) {
      var temp = value[0];
      value[0] = new Date(temp.getFullYear(),temp.getMonth(),temp.getDate());
    };
    console.log( new Date(values[0][0]) );
    console.log( new Date(values[1][0]) );
    console.log(values[0][0].valueOf() === values[1][0].valueOf());
    values.forEach( removeTime );
    console.log( new Date(values[0][0]) );
    console.log( new Date(values[1][0]) );
    console.log(values[0][0].valueOf() === values[1][0].valueOf());
  }
  catch(err) {
    console.log(err);
  }
}

9:15:47 AM  Notice  Execution started
9:15:47 AM  Info    Fri Feb 18 2022 11:53:45 GMT-0500 (Eastern Standard Time)
9:15:47 AM  Info    Fri Feb 18 2022 11:54:16 GMT-0500 (Eastern Standard Time)
9:15:47 AM  Info    false
9:15:47 AM  Info    Fri Feb 18 2022 00:00:00 GMT-0500 (Eastern Standard Time)
9:15:47 AM  Info    Fri Feb 18 2022 00:00:00 GMT-0500 (Eastern Standard Time)
9:15:47 AM  Info    true
9:15:48 AM  Notice  Execution completed

CodePudding user response:

sheetTimeStamp = Utilities.formatDate(sheetTimeStamp, Session.getTimeZone(), "MM/dd/yyyy HH:mm:ss")

change into:

sheetTimeStamp = Utilities.formatDate(new Date(sheetTimeStamp), Session.getTimeZone(), "MM/dd/yyyy HH:mm:ss")

it should work

  • Related