javascriptdatetimegoogle-apps-scriptdate-comparison

Google Aps Scripts - Set a value if input not in row


I'm trying to set an input value if this value isn't in a row. I can get the values of the row and also set the value.

My problem appears when I asked if the input is in the row. This is my script:

//Auxiliar Funtion
    function is_in_array(array,value) {
          is_in = false
          Logger.log('This is the value: ' + value)
          for (i = 0; i < array.length; i++) {
            Logger.log('This is i: ' + array[i])
            if (value == array[i]) {
                is_in=true;
                Logger.log('I was here!')
            }
          }
          return is_in
        }
        
//Main
function rm(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ws = ss.getActiveSheet();

  var fecha = ws.getRange(5,9).getValue();

  //Reviso si existe la fecha
  ws = ss.getSheetByName(tipo);
  numCols = ws.getLastColumn();
  fechas = ws.getRange(1,4,1,numCols).getValues()[0];
  Logger.log('Fecha :' + fecha);
  Logger.log('Fechas: ' + fechas);
  is_in_result = is_in_array(fechas, fecha)
  Logger.log('Functions result: ' + is_in_result)

  //Logger.log(ArrayUtils.contains( fechas, fecha ))
  if (is_in_result==true) {
    Logger.log('If statement')
    return True 
  } else {
    Logger.log('Else Statement')
    //Se añade la fecha
    ws.getRange(1,numCols+1).setValue(fecha)
  } 
}

Here is the log:

Registros de Cloud
9 ene 2022, 23:51:54    Información Fecha :Thu Sep 09 2021 03:00:00 GMT-0400 (hora de verano oriental)
9 ene 2022, 23:51:54    Información Fechas: Thu Sep 09 2021 03:00:00 GMT-0400 (hora de verano oriental),Fri Oct 15 2021 03:00:00 GMT-0400 (hora de verano oriental),Wed Dec 08 2021 03:00:00 GMT-0500 (hora estándar oriental),,,
9 ene 2022, 23:51:54    Información This is the value: Thu Sep 09 2021 03:00:00 GMT-0400 (hora de verano oriental)
9 ene 2022, 23:51:54    Información This is i: Thu Sep 09 2021 03:00:00 GMT-0400 (hora de verano oriental)
9 ene 2022, 23:51:54    Información This is i: Fri Oct 15 2021 03:00:00 GMT-0400 (hora de verano oriental)
9 ene 2022, 23:51:54    Información This is i: Wed Dec 08 2021 03:00:00 GMT-0500 (hora estándar oriental)
9 ene 2022, 23:51:54    Información This is i: 
9 ene 2022, 23:51:54    Información This is i: 
9 ene 2022, 23:51:54    Información This is i: 
9 ene 2022, 23:51:54    Información Functions result: false
9 ene 2022, 23:51:54    Información Else Statement

Fecha is in Fechas so the function result must be true, but its returns false :(

Where is my error? Or, there is an easier way to get the same result? Maybe it doesn't work because I'm comparing two dates, I don't know...

Some things are in spanish, sorry


Solution

  • You can't compare different Date objects with == or ===. Compare them by their milliseconds since Unix Epoch with Date.getTime()

    var date1 = new Date( '2022-01-01' );
    var date2 = new Date( '2022-01-01' );
    
    console.log( 'date1 == date2: ' + ( date1 == date2 ) ); // false
    console.log( 'date1 === date2: ' + ( date1 === date2 ) ); // false
    console.log( 'date1.getTime() == date2.getTime(): ' + ( date1.getTime() == date2.getTime() ) ); // true