-1

Could you help me please? I need to compare two dates to generate an if condition when it expires. My idea is to send an email to the person involved when today's date is greater than the date in the elements [16]. I broke my head and I couldn't compare with each other, it only works with the same because it is comparing text. An example below:

function sendmailAtualizarFollowup(){
var app = SpreadsheetApp;
var gmail = GmailApp;
var spreadsheet = app.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('Cópia de Dados');
var values = sheet.getRange("B2:AR").getValues();
var now = new Date();
   // var now = new Date();
   // var nowString = Utilities.formatDate(now, 'TimeZone', 'dd/MM/yyyy');
   // Logger.log(now);
 
values.map(function(elem,ind,obj){
if(elem[0] != ""){
var vencimento = new Date(elem[16]);{
   // var vencString = Utilities.formatDate(vencimento, 'timeZone', 'dd/MM/yyy');
   // Logger.log(vencString);
 
if(elem[11] != ""){

if(now.toDateString() == vencimento.toDateString()){   //Here's my trouble

gmail.sendEmail(elem[38], "Subject -> "+elem[0], "Example:  "+elem[5]+"  "+elem[7]+", sob responsabilidade do(a) "+elem[10]+". A data de emissão do relatório contendo este evento é "+elem[3].toDateString()+", e o vencimento original proposto pela área é "+elem[11].toDateString()+". A situação do ponto é: Controle "+elem[22]+". Os detalhes do apontamento estão disponíveis no link: "+elem[20]+". Esta é uma mensagem automática.");
}
}
}})};
Iamblichus
  • 18,540
  • 2
  • 11
  • 27
Leandro
  • 3
  • 2

2 Answers2

0

You only need to make both variables Date objects then compare them. Don't forget to use getTime() function on the dates before comparing as doing it directly will not work on some operators such as ==, !=, === and !==.

var d1 = new Date();
var d2 = new Date(d1);
if (d1.getTime() == d2.getTime()) {  // d1 == d2 will yield false
    Logger.log("dates are equal");
}

You can easily change the comparison operator based on what your script needs you to achieve.

NightEye
  • 10,634
  • 2
  • 5
  • 24
0

If you are looking for a way to convert date to number/integer, you can use the built-in function of Javascript called getTime(). See below sample for getting integer value of current date:

var now = new Date().getTime();

This function will convert your date into integer value specifically in milliseconds. This way, you can easily use comparison operators since both of your dates are in integer format.

Jason E.
  • 1,201
  • 1
  • 3
  • 10