Apps Script for Excel and Compare a date to today

3,563 views
Skip to first unread message

Randy Rands

unread,
Jan 14, 2022, 10:23:14 PM1/14/22
to Google Apps Script Community
Very new to apps script, but have searched, and can't find what I hope is an easy solution.  I am trying to compare a date from a cell in Sheets to today, and run a script if they match.  Here's what I have:

var data_range = sheet.getDataRange();
  var today= new Date();
  today.setHours(0,0,0,0);
  var row = 21 // row to look at for the upcoming meeting
    
  {
    var mtg_date = data_range.getCell(row,1).getValue(); // get the top date to compare to today's Date
    mtg_date.setHours(0,0,0,0);
    if(mtg_date = today)
    {

the issue is that as is, the line "if(mtg_date = today)" is actually setting mtg_date to today's value,.

If I change that line to "if(mtg_date == today)", then it just never evaluates as true, even if the dates match.

Any thoughts on what I am doing incorrectly, please? (or how to do the evaluation correctly)


Thank you in advance!

Scott Bennett

unread,
Jan 14, 2022, 10:39:26 PM1/14/22
to google-apps-sc...@googlegroups.com
You have to use the the double equal signs. That is a testing whether or not they are equal. 
Log things out using Logger.log(today) and Logger.log(mtg_date). 
Also look at using Utilities.formatDate. 

Scott Bennett


Sent from my iPhone 

On Jan 14, 2022, at 9:23 PM, Randy Rands <Ra...@rands.us> wrote:

Very new to apps script, but have searched, and can't find what I hope is an easy solution.  I am trying to compare a date from a cell in Sheets to today, and run a script if they match.  Here's what I have:
--
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/648e1ecf-66b8-4f25-9c0c-223c62649a3an%40googlegroups.com.

Randy Rands

unread,
Jan 14, 2022, 11:03:24 PM1/14/22
to Google Apps Script Community
thanks, Scott - here is what I am getting - it still is skipping over my == statement:

Output of logger:
8:56:49 PM   Notice   Execution started
8:57:11 PM   Info        value of today is: Fri Jan 14 2022 00:00:00 GMT-0500 (Eastern Standard Time)
8:57:11 PM   Info        value of mtg_date is: Fri Jan 14 2022 00:00:00 GMT-0500 (Eastern Standard Time)

And my code:

var today= new Date();
  today.setHours(0,0,0,0);
{
var mtg_date = data_range.getCell(row,1).getValue(); // get the top date to compare to today's Date
  mtg_date.setHours(0,0,0,0);
Logger.log ('value of today is: ' +today)
Logger.log (' value of mtg_date is: ' +mtg_date)
if(mtg_date == today)
    {
      var duties = getDuties(row);
        ...

but it evaluates "if(mtg_date == today)" as false, even though they look to be the same to me?

What do you think?

Randy Rands

unread,
Jan 14, 2022, 11:44:51 PM1/14/22
to Google Apps Script Community
OK - I got it - seems like this would be a pretty common thing, so not really sure why it was so difficult to run down, but...  Here's what worked for me:

var today = Utilities.formatDate(new Date(), 'GMT-7', 'MM-dd-yy');
  {
    var mtg_date = data_range.getCell(row,1).getValue(); // get the top date to compare to today's Date
    mtg_date = Utilities.formatDate(mtg_date, 'GMT-7', 'MM-dd-yy');
    if(mtg_date == today)
    {



Thank you very much for the help - it definitely got me on the right track!  :)

CBMServices Web

unread,
Jan 14, 2022, 11:57:18 PM1/14/22
to google-apps-sc...@googlegroups.com
The problem with date values is that they also include hour, mins, secs and millisecond values.

If you are comparing just the dates, you will need to check it separately from checking the entire structure.

Use getFullYear() getMonth() getDate() and then compare values and see if they match.


--
Reply all
Reply to author
Forward
0 new messages