我试图调整这个答案Parsing date strings using google apps script,但我迷路了!
function test_t(){
var formattedDate = Utilities.formatDate(new Date(), "GMT+2", "yyyy-MM-dd-HHmm");
var date = (new Date(formattedDate)).toDateString();
// // //var dateParts = formattedDate.split('-');
//var dateParts = formattedDate.split(',');
//var fromDate = new Date(dateParts[0], dateParts[1], dateParts[2], dateParts[3]);
Logger.log(date)
}
提前致谢;)
编辑:道歉。我将来会尝试更加明确:
以下作品。结果很好:2014-08-19-1350。
function test_t4(){
var formattedDate = Utilities.formatDate(new Date(), "GMT+2", "yyyy-MM-dd-HHmm");
Logger.log(formattedDate)
}
但是当我在更复杂的代码中使用相同的东西时,我有相同的"无效的日期"文件名中的错误。
function copyModel2() {
// The code tries to create a new spreadsheet (renamed with text+date-and-time) into a specific folder
//(inspired from https://stackoverflow.com/questions/16418109/how-can-i-create-spreadsheet-and-open-it-automaticclly)
var formattedDate = Utilities.formatDate(new Date(), "GMT+2", "yyyy-MM-dd-HHmm");
var date = (new Date(formattedDate)).toDateString();
var source = SpreadsheetApp.openById("0A...key..5UFE");
var ssname = source.getSheetByName("main").getRange("A1").getValues();
var date_ssname = ssname +"_"+ date
//var folder = DriveApp.getFoldersByID('0B...folderID....U'); // For some reasons I have always an error if I use the correct folder ID
var folder = DriveApp.getFoldersByName('specific_folder_name'); // replace by the right folder name, assuming there is only one folder with this name
var destination = SpreadsheetApp.create(date_ss_name);
// result for the new sheet name: "rangeA1text_Invalid Date"
// desired result : rangeA1text_2014-08-19-1343
// I succeeded in creating the correct spreadsheet name once but any other trials leads to "rangeA1text_Invalid Date" error in the spreadsheet filename.
}
提前致谢;)
答案 0 :(得分:1)
这是它的工作原理:
function test_t(){
var formattedDate = Utilities.formatDate(new Date(), "GMT+2", "yyyy-MM-dd-HH-mm");
var dateParts = formattedDate.split('-');
var date = (new Date(dateParts[0], dateParts[1]-1, dateParts[2], dateParts[3], dateParts[4],0,0)).toDateString();
// new Date is defined with parameters year,month,day,hours,minutes,seconds, milliseconds as separate integers
Logger.log(date)
}
就像那样(代码中的注释):
var formattedDate = Utilities.formatDate(new Date(), "GMT+2", "yyyy-MM-dd-HHmm");// this is a string, not a date anymore >> you can use it directly to build the name of your file.
var source = SpreadsheetApp.openById("0A...key..5UFE");
var ssname = source.getSheetByName("main").getRange("A1").getValues();
var date_ssname = ssname +"_"+ formattedDate