Comparing Timestamps

1,289 views
Skip to first unread message

Nigel Hunt

unread,
Mar 28, 2022, 12:15:53 PM3/28/22
to Google Apps Script Community
I need to compare 2 Timestamps on different sheets.
They both show as  28/02/2022 11:32:02 in the formula bar.
Placing this formula  =if(A11=Responses!A46, "Cells Match","Cells don't Match") in a nearby cell shows that the two Timestamps, (or cell value?) are equal.
This script.....
function TimeTest(){
  var ss = SpreadsheetApp.getActive().getSheetByName('Math');
  ss.activate();
  var day1 = SpreadsheetApp.getActiveSheet().getRange(11, 1).getValue();
  SpreadsheetApp.getUi().alert(day1);
  var ss = SpreadsheetApp.getActive().getSheetByName('Responses');
  ss.activate();
  var day2 = SpreadsheetApp.getActiveSheet().getRange(46, 1).getValue();
  SpreadsheetApp.getUi().alert(day2);
  if (day2 === day1) {
   SpreadsheetApp.getUi().alert("Timestamps Match");
  }
   if (day2 !== day1) {
   SpreadsheetApp.getUi().alert("Timestamps Do Not Match");
  } 
}
says they DON'T match.

The alerts both also show -0500 GMT, & suggest its about 630, which is a bit confusing.
The data was copied from a worksheet originating in USA if that helps.
Can anybody explain me why please, &/or suggest a solution?
I've done dozens of searches & found nothing this specific.
Thanks
Nigel


Jonathan Butler

unread,
Mar 28, 2022, 5:04:32 PM3/28/22
to google-apps-sc...@googlegroups.com
Considering there are two spaces in between the word as and the date in your email (" They both show as  28/02/2022 11:32:02 in the formula bar.  ") Maybe you should add a trim function to get rid of erroneous spaces added on to it. If that's not the issue, I'm not sure what could be.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/a0261e6c-8e65-4096-aace-a3824bb379e8n%40googlegroups.com.

Nigel Hunt

unread,
Mar 29, 2022, 9:03:05 AM3/29/22
to Google Apps Script Community
I've checked for spaces in the cell; there are none.
The issue is still neither = nor == perform as I would logically expect

Edward Ulle

unread,
Mar 29, 2022, 9:52:24 AM3/29/22
to Google Apps Script Community
Try this  if (day2.valueOf() === day1.valueOf()) {

Nigel Hunt

unread,
Mar 29, 2022, 10:10:47 AM3/29/22
to Google Apps Script Community
Works perfectly. Thanks very much thew...

Edward Ulle

unread,
Mar 29, 2022, 11:17:10 AM3/29/22
to Google Apps Script Community
When you getValues() of a cell containing a date the value is a Date() object.  So comparing Date() objects is not equal, you have to get the valueOf() the Date() object.
Reply all
Reply to author
Forward
0 new messages