Home > Blockchain >  Logger.log(Date inside Cell) keeps defaulting to today's date (Google Apps Script)
Logger.log(Date inside Cell) keeps defaulting to today's date (Google Apps Script)

Time:12-17

Here's what I want to do:

I want to take a date inside a cell, and then increment a RANDOM number of seconds (between 0 and 60 or something like that) to the Date() value inside the cell. I want to do this using Google Apps Script.

var dateRange = sheet.getRange(dateRow, dateCol);
var date = dateRange.getValue();

dateRange.setValue(Date(date   Math.random()));
dateRange.setValue(new Date()).setNumberFormat("MM/dd/yyyy HH:mm:ss");

After it sets the date to today's date for some reason?

Logger.log(Date(date)); // Outputs Todays Date..?

Is this a time zone conflict? I have the spreadsheet set to Pacific Time.

CodePudding user response:

I thought that in your script, by dateRange.setValue(new Date()), the current date is put. I thought that this might be the reason of your issue. So, in your situation, how about the following modification?

From:

dateRange.setValue(Date(date   Math.random()));
dateRange.setValue(new Date()).setNumberFormat("MM/dd/yyyy HH:mm:ss");

To:

var newDate = date.getTime()   (Math.floor(Math.random() * 60) * 1000);
dateRange.setValue(new Date(newDate)).setNumberFormat("MM/dd/yyyy HH:mm:ss");
  • Related