Search code examples
javascriptdatetimegoogle-apps-scriptgoogle-forms

Compare timestamp in two different formats - GAS


I am using the enhanced workflow script that was posted by Mogsdad here.

I have managed to work out a few issues but one that I am stuck on at the moment is the error that comes up from this section -

// Record approval or rejection in spreadsheet
  var row = ArrayLib.indexOf(data, 0, timestamp);
  if (row < 0) throw new Error ("Request not available.");  // Throw error if request was not found
  sheet.getRange(row+1, approvalCol).setValue(e.parameter.approval);

I get the "Request not available" error because the ArrayLib.indexOf object is comparing the time stamp that is being presented from the same source but via two different 'routes'.

The timestamp from the 'timestamp' variable looks like this - "17/03/2015 18:00:11"

...and the timestamp contained in the 'data' variable (that should match the timestamp variable) looks like this - "Tue Mar 17 2015 00:30:10 GMT-0700 (PDT)".

I am assuming that the two different formats is what is resulting in the ArrayLib.indexOf object returning a '-1' result and hence the error message.

Any thoughts on what I need to do to get the matching working successfully ?


Solution

  • Create a new Date object for the timestamp value, so that you can ensure they can be compared. The code should look like:

      var dateFromTimestamp = new Date(timestamp);