Subtraction with date - Google Apps Script

0

Expensive,

I need to calculate how many days remain to reach the target date.

I'm getting the following values

var dataPrev = row[4]; //26/01/2018
var dataNaoFormat = new Date(); //22/01/2018

I'm doing the following calculation:

var sub = dataPrev - dataNaoFormat;

And the result is:

2.65807814E8

Taking the scientific format, 26,580,781,400,000,000.00

I need to get an integer back, in which case it would be 4.

    
asked by anonymous 23.01.2018 / 01:16

2 answers

0
    var row = data[i];
    var dataCarimbo = Utilities.formatDate(new Date(row[0]), "GMT", "dd/MM/yyyy");
    var dataForm = new Date(row[1]);
    var email = row[2];
    var dataPrev = row[4];
    var opcao = row[3];

    var timeDiff = Math.abs(dataPrev.getTime() - dataForm.getTime());
    var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));

Seen in topic: Calculate difference between two dates to validate date fields

    
23.01.2018 / 01:35
0

Can not you use any js library to handle these dates? There is a call momentJs link

With it you can handle this very easily.

Here's an example in php, too, if you can:

$date = new DateTime("NOW");
echo $date->format('Y-m-d')."<br>";

//subtrai mês da data atual
$date = $date->sub(DateInterval::createfromdatestring('+ 1 month'));
echo 'Subtraindo 1 mês: '.($date->format('Y-m-d')).'<br>'; 

//Adiciona 1 mês a data
$date = $date->add(DateInterval::createfromdatestring('+ 1 month'));

//Recarrega mês atual
echo 'Adicionando 1 mês'.($date->format('Y-m-d')).'';
    
23.01.2018 / 02:04