Home > Blockchain >  getDatecreated in Appps Script returning the time of creation an hour later
getDatecreated in Appps Script returning the time of creation an hour later

Time:06-26

When I try to get the time of creation of a file in Google Drive with the built in function getDateCreated() in the apps script, it returns the time an hour after the creation. My code is as follows

var folder = DriveApp.getFolderById(myFolderId);

  var contents = folder.getFiles();
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  var var_file;
  var var_name;
  var var_link;
  var var_owner;
  var var_time;

  while(contents.hasNext()){
    var_file = contents.next();
    var_name = var_file.getName();
    var_link = var_file.getUrl();
    var_owner = var_file.getOwner().getName();
    var_time = var_file.getDateCreated();
    sheet.appendRow([var_name,var_link,var_owner, var_time]);
  }

If time of creation of a file is 25/06/2022 22:48:39 The output of my code returns 25/06/2022 23:48:39 which is an error. Note: My time zone is UTC-5

CodePudding user response:

The only reason could be the time zone setup of your script file. You can check out bi going to File > Project properties in the script editor.

Google developers docs

Also you can review this setting running this:

var timeZone = Session.getScriptTimeZone();
Logger.log(timeZone);

CodePudding user response:

I did some research on different built-in methods in Javascript and the .toLocaleTimeString() seems to work fine returning the actual time the file was created.

var_time = var_file.getDateCreated().toLocaleTimeString();

It's also quite important to check the appsscript.json file which can be accesed by going to Project Settings and toggling on the option Show the appsscripts.json file

  • Related