Title: Comparing Dates in Google Scripts with Sheets Input
Tags: date;google-apps-script;comparison
Question: I am trying to create a pop up to warn a user they must update a part in inventory if the part date is more than 90 days old. The date on the sheet (Cell Q5) is autofilled from another sheet, but that shouldn't matter. The value for the cell on the spreadsheet is 9/2/2021. I've tried many things, but currently I am getting the value for Q5 showing up as NaN .
function CheckInvDate() {
var ss = SpreadsheetApp.getActive().getId();
var partsrange = Sheets.Spreadsheets.Values.get(ss, &quot;BOM!A5:Q5&quot;);
var currentDate = new Date();
var parthist = new Date();
parthist.setDate(currentDate.getDate() -90);

for (var i = 0; i < partsrange.values.length; i++){
var name = partsrange.values [i][1]
var partdate = partsrange.values [i][16]
var parthisttime = new Date(parthist).getTime();
var partdatetime = new Date(partdate).getTime();
Logger.log(&quot;History &quot; + parthisttime)
Logger.log(&quot;Current &quot; + partdatetime)
SpreadsheetApp.flush()

//    if (parthist &gt; partdate == &quot;TRUE&quot;) {
//      SpreadsheetApp.getUi().alert('The price on '+ name + ' is out of date. Please update price and try again.')
//    }
My last log was
[22-07-06 11:50:55:851 EDT] History 1649346655850
[22-07-06 11:50:55:853 EDT] Current NaN
I've seen a number of responses on Stack Overflow, but I can't understand them. They seem to refer to variables that I don't see in code, or commands I haven't seen before and I'm not sure if they are in date.
Comment: @gloo.  It shows as the value from cell Q5, given as 9/2/2021

Comment: Are you able to log what `partdate` was?


Here is another answer: I went to try this on my script again after lunch, and for whatever reason I am no longer getting the NaN value.  I made one change on the if statement to fix the logic, and now it is working correctly. Not sure what I did, but apparently the coding gods were unhappy with me before.
The only part I changed was
if (parthisttime &gt; partdatetime) {
SpreadsheetApp.getUi().alert('The price on '+ name + ' is out of date. Please update price and try again.')

Here is another answer: Try this:
function CheckInvDate() {
const ss = SpreadsheetApp.getActive();
const vs = Sheets.Spreadsheets.Values.get(ss.getId(), &quot;BOM!A5:Q5&quot;).values;
let d = new Date();
d.setDate(d.getDate() - 90)
const dv = d.valueOf();
const oldthan5 = vs.map(r =&gt; {
if (new Date(r[16]).valueOf() < dv) {
return r;
}).filter(e =&gt; e);
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(`<textarea rows=&quot;12&quot; cols=&quot;100&quot;&gt;${JSON.stringify(oldthan5)}</textarea&gt;`).setWidth(1000), &quot;Older Than 90 Days&quot;);
This outputs a dialog with the rows older than 90 days

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:07/06/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<date><google-apps-script><comparison>', 'Title': 'Comparing Dates in Google Scripts with Sheets Input', 'ViewCount': 24, 'id': 72887355, 'user_AboutMe': "<p>Independent learner of Google App Script Code with thoughts to moving to wider JavaScript at some point.  I'm using what I learn to try to automate processes at a small company that uses Google Docs and Google Sheets for their regular operations.</p>\n", 'user_AccountId': 17186548.0, 'user_DisplayName': 'Stephen Schonewolf', 'user_Reputation': 9.0, 'user_Views': 12.0, 'user_id': 12441062.0}}