I'm working on a small project, using Google Sheets and Google Script to generate a tennis scheduling system. Basically the user enters their info on a Google Form, which is passed to Google Sheets where the script will generate or append the user's input to a new or existing date slot Sheet. I'm trying to figure out what I'm doing wrong with my date validation code here. The idea is that 'if user's entered date is older than today's date, write down an error message in the corresponding Form Responses row and do not process the entry. However, the date comparison is not working as I expected. Here is the code snippet that I'm using to accomplish that, with Boolean tests to show what I'm getting when I do said date comparison:
var now = Utilities.formatDate(new Date(),"GMT+8","MMM/dd/yyyy");
/** Retrieve player name, timeslot, court slot from latest form entry */
var lastRow = sheet.getLastRow();
var lastRowRange = sheet.getRange(lastRow, 1, 1, 6);
var lastRowValues = lastRowRange.getValues();
/** current date format - MMM/dd/yyyy */
var dateStr = lastRowValues[0][2];
var dateSlot2 = sheet.getRange(lastRow,3);
var dateSlot = Utilities.formatDate(dateStr, "GMT+8", "MMM/dd/yyyy");
dateSlot2.setValue(new Date()).setNumberFormat("MMM/dd/yyyy");
var timeSlot = lastRowValues[0][3];
var courtSlot = lastRowValues[0][4];
/** test cases */
var dateTest = (dateSlot >= now); //always returns false
var dateTest2 = (dateSlot2 >= now); //always returns true
This is my first time with Google Script, so any advice is greatly appreciated!