Jenna Forfitt
Jenna Forfitt

Reputation: 31

Having issues with pulling a date from the spreadsheet

I am working with Google Scripts and here is my problem.

I am attempting to compare today's date with a date entered into column C in the spreadsheet. The code I have seems like it should work but when I use the logger to see the date it gives me "Wed Dec 31 19:00:00 GMT-05:00 1969" instead of 2018-07-29 1:00 PM.

I know I need to use the Utilities.formatDate in order to compare them but I can't seem to understand why it is not pulling the date in column C.

Here is my code below:

function sendEmail() {
 var sheet = SpreadsheetApp.getActiveSheet();
 var startRow = 3;  // First row of data to process
 var numRows = sheet.getLastRow()-1;   // Number of rows to process
 // Fetch the range of cells A2:B3
 var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
 // Fetch values for each row in the Range.
 var data = dataRange.getValues();
 //Logger.log(data); // give you the data you are looking at comparing


for (var i in data) {
 var row = data[i];
 var date = new Date();
 //Logger.log(date); // sets to todays date and time.
 var sheetDate = new Date(row[3]);
 Logger.log(sheetDate); // date in the row you are comparing

Upvotes: 2

Views: 84

Answers (1)

Diego
Diego

Reputation: 9571

You're not looking at the value in column C. The method getValues() returns a two-dimensional array, and arrays are 0-indexed. This means that you should be using a 2 (not a 3), when defining sheetDate.

var sheetDate = new Date(row[2]);

As mentioned in the comments, you don't need to use Utilities.formatDate(), but that's a separate issue. I would also try logging the value before converting it to a date (e.g. Logger.log(row[2])).

Upvotes: 1

Related Questions