0

Do IF statements work using timestamps? I'm using a Google Sheet from a Google Form and would like to use the Timestamp as a matching identifier.

Code:

if (time == homeTime) {
 s.getRange(7, 1).setValue("Yay!");

Logger.log shows both variables are indeed the same value (Tue Jun 26 08:47:52 GMT-04:00 2018).

It works if I choose a value other than the timestamp, such as an email address.

I cannot use < or > because I'm using this in a for loop. As a temporary fix, I changed the format of the column to Plain Text using @, but time - homeTime == 0 is the answer I needed!

I'm trying to figure out how to mark my answer correctly so I apologize if I mess it up.

  • 1
    Both are basically Date objects. So to be equal, all their inherent properties should be equal. Search for checking that if two dates are equal on SO and you will find your answer. – Suhail Ansari Jun 26 '18 at 16:47
  • 2
    You cannot compare date objects with the equality operator (== or ===) because two object instances are never the same. You can use <, >, <= and >= though. Use the timestamp value of the dates for comparison, e.g. date1.getTime() == date2.getTime(), but beware of possible time zone differences. – marekful Jun 26 '18 at 16:47
  • To clarify @marekful s comment about timezones - `getTime` is always in reference to UTC. However, the script time zone and the time zone of whatever serves the other `Date` input may be in a different tz. Your safest bet is to either always work with UTC milliseconds, or always include the tz info in any date string you use to create a `Date` object. – tehhowch Jun 26 '18 at 19:30
  • I believe you can also use `time - homeTime == 0` – TheMaster Jun 26 '18 at 21:08
  • Possible duplicate of [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – TheMaster Jun 26 '18 at 21:10
  • I am unable to use < or > because I'm using it in a for loop. I had a temporary fix of changing the cell format to text via script, but time - homeTime == 0 isthe answer I needed. Thanks@tehhowch and thanks everyone else for taking the time to help. PS. I apologize if I don't mark my answer correctly, but this UI is confusing. – Nolan Hellard Jun 27 '18 at 17:59

0 Answers0