Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

107
Views
How to compare dates with day, month and year only

I'm trying to go over a column that has some random dates and find todays date then save that row in a variable. I honestly wrote the code to do that, but for some reason its not working with which I don't know why.

This is my code.

 const ss = SpreadsheetApp.openByUrl(THIS).getSheetByName("Daily PO Dashboard");
  let birthday = new Date(2022, 1, 21,3,00,00)    
  for(var row=0; row<data.length; row++){
  if(data[row][16]==birthday){
    Logger.log(" I'm in!")
      ss.getRange('B3:AA').setValue(data[row]);
  }
  }
  var checkBox = ss.getRange('A3:A').insertCheckboxes();

I also tried to set the birthday as

var birthday = new Date();

but nothing is working with me.

This is what I'm comparing with.

[this is column 16 and each row is row

Note: it never goes in the if statement

this is data[row][16] when I logger.log it.

enter image description here

Note: there are more but they are all either dates or null

Also I logger.log(typeof data[row][17]), and i got a string.

enter image description here

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

Using strings and Utilities.formatDate() seems kind of brute force to me. If I have to compare dates I remove the time and then use valueOf() to compare dates.

function dummy() {
  try {
    var d1 = new Date();
    Utilities.sleep(100);
    var d2 = new Date();
    console.log(d1);
    console.log(d2);  // Notice in console log they look alike but are different by 100 msec.
    console.log( d1.valueOf() === d2.valueOf() );
    function removeTime(date) {
      return new Date(date.getFullYear(),date.getMonth(),date.getDate());
    }
    d1 = removeTime(d1);
    d2 = removeTime(d2);
    console.log(d1);
    console.log(d2);
    console.log( d1.valueOf() === d2.valueOf() );
  }
  catch(err) {
    console.log(err);
  }
}

2:46:07 PM  Notice  Execution started
2:46:07 PM  Info    Mon Feb 21 2022 17:46:07 GMT-0500 (Eastern Standard Time)
2:46:07 PM  Info    Mon Feb 21 2022 17:46:07 GMT-0500 (Eastern Standard Time)
2:46:07 PM  Info    false
2:46:07 PM  Info    Mon Feb 21 2022 00:00:00 GMT-0500 (Eastern Standard Time)
2:46:07 PM  Info    Mon Feb 21 2022 00:00:00 GMT-0500 (Eastern Standard Time)
2:46:07 PM  Info    true
2:46:07 PM  Notice  Execution completed
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.