简体   繁体   English

如何在获取过去 email 的日期后格式化日期?

[英]How to format date after getting the date of a past email?

I'm wiring a script that pulls CSV files from my email and moves them to a drive folder for easier importing, and I want to rename the files to the date of the email as MMddyyyy, however, when I format the date after using getDate(), the file is renamed to date that the script is being ran and not the date the email was received.我正在连接一个脚本,从我的 email 中提取 CSV 个文件并将它们移动到驱动器文件夹以便于导入,我想将文件重命名为 email 的日期作为 MMddyyyy,但是,当我在使用 getDate 后格式化日期时(),文件重命名为运行脚本的日期,而不是收到 email 的日期。

//const fileType = ['csv'];
//const processedLabel = "Processed TBD Data";
//var res = GmailApp.search(query);
const dir = DriveApp.getFolderById("<folderID>");
const query = 'has:attachment from:it label:EOD-TBD';
var rmLabel = GmailApp.getUserLabelByName('EOD-TBD');
var newLabel = GmailApp.getUserLabelByName('Processed TBD Data');

function countThreads() { 

  //Find threads within "EOD-TBD" that have an attachment
  var TBD_Threads = GmailApp.search(query);

  for (var m = 0; m < TBD_Threads.length; m++) { //Counts threads
  var threads = TBD_Threads[m].getMessages()
  
  for (var j = 0; j < threads.length; j++) {
    var date = threads[j].getDate()
    var nDate = Utilities.formatDate(new Date(date), "GMT+1", "MM/dd/yyyy")
    Logger.log("Processing Message from: " + nDate)
    var att=threads[j].getAttachments()
    for (var k = 0; k < att.length; k++){
      var attachmentBlob = att[k].copyBlob().setName(nDate);
      //var file = DriveApp.createFile(attachmentBlob);
      var file = dir.createFile(attachmentBlob);

    }
  }
  TBD_Threads[m].removeLabel(rmLabel);
  TBD_Threads[m].addLabel(newLabel);
}
}

I can't, for the life of me, find out how to format the date properly.在我的一生中,我无法找到如何正确设置日期格式的方法。 If I log 'date', it'll show the correct date of the email that's being processed, but it's showing it as [day][numbered day][month][year][time].如果我记录“日期”,它会显示正在处理的 email 的正确日期,但显示为 [日][编号日][月][年][时间]。

I fixed it.我修好了它。 var nDate = Utilities.formatDate(new Date(date), "GMT+1", "MM/dd/yyyy") to var nDate = Utilities.formatDate(date, "GMT-0600", "MM/dd/yyyy") var nDate = Utilities.formatDate(new Date(date), "GMT+1", "MM/dd/yyyy")var nDate = Utilities.formatDate(date, "GMT-0600", "MM/dd/yyyy")

If I understand correctly, the timezone that getDate() was using was different than the timezone declaration in formatDate(), so it was subbing todays date?如果我理解正确,getDate() 使用的时区与 formatDate() 中的时区声明不同,所以它是今天的日期? Either way, once I changed the timezones to match, it formatted correctly with the date of the email.无论哪种方式,一旦我更改了时区以匹配,它就会正确地格式化为 email 的日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM