skip to Main Content

I am trying to find the number of days between the current date and file creation date. The idea is to delete the files that are not more than 4 days from current date.

I used the below app script but the difference gives me a ‘NaN’ output in console


function iterateFolder() {
  var folder = DriveApp.getFolderById('XXXXXXXXXXX'); 
  console.log('Folder: ', folder.getName());
  var files = folder.getFiles(); 

  var currentDate = new Date();
  var modifiedDate = Utilities.formatDate(currentDate, 'Australia/Adelaide', 'dd-MM-yyyy');
  console.log('Now: ', modifiedDate);
  

  while(files.hasNext()){ 
    var file = files.next(); 
    var createdDate = file.getDateCreated();
    var modifiedCreatedDate = Utilities.formatDate(createdDate, 'Australia/Adelaide', 'dd-MM-yyyy');
    console.log('File: ', file.getName(), ' Created On: ', modifiedCreatedDate);

    console.log('Difference: ', modifiedDate - modifiedCreatedDate); // Gives NaN

  }

}

2

Answers


  1. Get the days between two dates

    This modified version of your code will give you the difference(in days) between the two dates

    function iterateFolder() {
      var folder = DriveApp.getFolderById('replace_with_your_drive_folder_id');
      console.log('Folder: ', folder.getName());
      var files = folder.getFiles();
    
      var currentDate = new Date();
      console.log('Now: ', currentDate);
    
      while (files.hasNext()) {
        var file = files.next();
        var createdDate = file.getDateCreated();
        console.log('File: ', file.getName(), ' Created On: ', createdDate);
    
        // Calculate the difference in milliseconds
        var differenceMillis = currentDate - createdDate;
    
        // Convert milliseconds to days
        var differenceDays = Math.floor(differenceMillis / (1000 * 60 * 60 * 24));
        console.log('Difference in days: ', differenceDays);
      }
    }
    

    Sample Output

    sample output

    References: Calculate difference between dates

    Login or Signup to reply.
  2. Apps Script does not treat dates as strings. The Date object in Apps Script is built on the Date object in JavaScript, which is…a Date object. Doing maths with dates is possible, as shown in the following very simple snippet:

    const today = new Date();
    const judgementDay = new Date('1997-08-29T06:14:00');
    console.log(`Milliseconds elapsed since the rise of Skynet: ${today - judgementDay} ms.`);

    In fact, that is exactly what 4thAnd1 is doing in his answer.

    The problem is that in your code, Utilities.formatDate converts a date into a string. And arithmethic operations with strings are not possible.

    The recommended way to to maths with dates is using Date.getTime. So your code should be something like:

    function iterateFolder() {
      var folder = DriveApp.getFolderById('XXXXXXXXXXX'); 
      console.log('Folder: ', folder.getName());
      var files = folder.getFiles(); 
    
      var currentDate = new Date();
      var modifiedDate = Utilities.formatDate(currentDate, 'Australia/Adelaide', 'dd-MM-yyyy');
      console.log('Now: ', modifiedDate);
    
      while(files.hasNext()){ 
        var file = files.next(); 
        var createdDate = file.getDateCreated();
        var modifiedCreatedDate = Utilities.formatDate(createdDate, 'Australia/Adelaide', 'dd-MM-yyyy');
        console.log('File: ', file.getName(), ' Created On: ', modifiedCreatedDate);
    
        const MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
        console.log('Difference: ', Math.floor((currentDate.getTime() - createdDate.getTime()) / MILLIS_IN_DAY));
    
      }
    }
    

    References

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search