0

I have tried to match tomorrow's date with the date that gets submitted via google form. in the logger it appears to match but it wont log YES and doesnt evaluate true.

My effort:

function ArchiveTuesdayOrder() {
  let dt = new Date();
  let t = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate() + 1);//default for items not provided in the constructor is zero
 
  const date = new Date();
  date.setDate(date.getDate() + 1);
  var tom = Utilities.formatDate(date, "America/New_York", 'EEE MMM dd yyyy HH:mm:ss Z');
  var tomDate = Utilities.formatDate(date, "America/New_York", "MM-dd-yyyy");
  var sheetActive = SpreadsheetApp.openById("xxxxxxxxxxxxxx");
  var sheet = sheetActive.getSheetByName("xxxxxxxxxxxxxx");
  //var orderDate = sheet.getRange(2,3,100).getValues();
  var orderdateRange = sheet.getRange(2, 4, 100);
  var orderDate = orderdateRange.getValues();

  Logger.log(tom.substring(0,10))
  Logger.log(t);
  for (var i = 0; i < orderDate.length; i++) {

  Logger.log(orderDate[i])
 if (t === orderDate[i]) {    // This is what I cant get to evaluate true- No Match
    Logger.log("YES"+orderDate)
  }}}
OblongMedulla
  • 1,471
  • 9
  • 21
  • I don't think that is a good idea to convert a date to text to compare it with another date. Instead use Date.prototype.getTime() to get the dates representation in milliseconds and use them to do the comparison. If you need further help, 1. add a [mcve] including sample input data and the expected result as well a brief description of your search efforts as is suggested in [ask]. – Rubén Oct 12 '21 at 14:17
  • Possible duplicate: https://stackoverflow.com/q/492994/1595451 – Rubén Oct 12 '21 at 14:18

1 Answers1

1
function tomorrow() {
  let dt = new Date();
  dt.setDate(dt.getDate() + 1);
  Logger.log(dt);
  return dt.valueOf();//milliseconds can be used in numerical comparisons
}

function tomorrowstartofday() {
  let dt = new Date();
  let t = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate() + 1);//default for items not provided in the constructor is zero
  Logger.log(t);
  return t.valueOf();
}

Here's a small example of picking timestamp that occur within a day:

The fake data:

TimeStamp day type
10/11/2021 0:00:00 yesterday
10/11/2021 12:00:00 yesterday
10/12/2021 0:00:00 start of day
10/12/2021 12:00:00 today
10/13/2021 0:00:00 tomorrow
10/13/2021 12:00:00 tomorrow

The code:

function timestampsfortoday() {
  const dt = new Date();
  const tod = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//today
  const tom = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate() + 1).valueOf();//tomorrow
  let ts = [];
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet0');
  const vs = sh.getRange(2,1,sh.getLastRow() - 1,2).getValues();
  vs.forEach(r => {
    let d = new Date(r[0]).valueOf();//using Date() constructor to get date value from timestamp
    if(d > tod && d < tom) {
      ts.push(r);
    }
  });
  Logger.log(ts.join('\n'))
}

The Execution Log:

9:58:44 AM  Notice  Execution started
9:58:46 AM  Info    Tue Oct 12 2021 12:00:00 GMT-0600 (Mountain Daylight Time),today
9:58:45 AM  Notice  Execution completed

It only picked the row with today in the second column because that's the only one between start of day today and start of day tomorrow.

If you use this line for the comparison in the loop:

if(d >= tod && d < tom)

You get this:

Execution log
10:05:58 AM Notice  Execution started
10:05:59 AM Info    Tue Oct 12 2021 00:00:00 GMT-0600 (Mountain Daylight Time),start of day
Tue Oct 12 2021 12:00:00 GMT-0600 (Mountain Daylight Time),today
10:05:59 AM Notice  Execution completed
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • This is close, I like that it has the time of 00:00 but I cant get it to evaluate as a match with the timestamp in the cell of google sheets. The Log shows: Wed Oct 13 00:00:00 GMT-05:00 2021 and the value from the cell is showing [Wed Oct 13 00:00:00 GMT-05:00 2021] in the logger. – OblongMedulla Oct 12 '21 at 15:24
  • 2
    If you looking to get all of the timestamps in a given day then you want to convert the timestamp to value with the Date() constructor and then look for date between today start of day and tomorrow start of day. – Cooper Oct 12 '21 at 15:38
  • The time really doesn't matter- it's not being used at all- it's just part of the timestamp on the google form. I really only care if the date matches- Is this possible? – OblongMedulla Oct 12 '21 at 15:45
  • I edited my example and provide all the code- I am really only wanting to match dates- but google provides a timestamp as well – OblongMedulla Oct 12 '21 at 15:58
  • 1
    It all depends upon what you want. In my case I wanted all of the timestamps greater than start or day today and less start of day tomorrow. – Cooper Oct 12 '21 at 16:01
  • 1
    In the last example I changed the comparison to accept those timestamps that are exactly on start of day. The point is that if you convert it to a date value you have a lot more control in picking the submissions that you wish to look at. – Cooper Oct 12 '21 at 16:09
  • 2
    I admit I don't want to give a script you can just copy without even having to think about it because I've done that a lot in the past and you end up with a bunch users that end up learning nothing and they continue to return asking for changes that they should be able to figure out on their own which makes answering questions here really boring. – Cooper Oct 12 '21 at 16:12
  • thanks I appreciate it- I was able to wrap my head around it thanks for the information and help! – OblongMedulla Oct 12 '21 at 17:02