Datetime 在谷歌应用程序脚本中使用日期、时间
我需要一些与谷歌应用程序脚本快速编码的帮助,链接到我的谷歌电子表格 在谷歌电子表格中,我有一个单元格的值为“26-Jun-2020”。这是一个约会。 我想用谷歌应用程序脚本计算那个日期(“2020年6月26日”)和今天之间的天数差,但不知怎么的,它不会帮我计算 如果我使用Logger.log(expiration\u date[I])只打印“expiration\u date[I]”,它将提供输出“Fri Dec 17 2021 01:00:00 GMT-0500(东部标准时间)”Datetime 在谷歌应用程序脚本中使用日期、时间,datetime,google-apps-script,Datetime,Google Apps Script,我需要一些与谷歌应用程序脚本快速编码的帮助,链接到我的谷歌电子表格 在谷歌电子表格中,我有一个单元格的值为“26-Jun-2020”。这是一个约会。 我想用谷歌应用程序脚本计算那个日期(“2020年6月26日”)和今天之间的天数差,但不知怎么的,它不会帮我计算 如果我使用Logger.log(expiration\u date[I])只打印“expiration\u date[I]”,它将提供输出“Fri Dec 17 2021 01:00:00 GMT-0500(东部标准时间)” 函数Put\
函数Put\u Options\u expiration\u Alert(){
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getSheetByName(“长期股权(卖出期权)”);
//var timeZone=AdsApp.currentAccount().getTimeZone();//获取当前电子表格的时区
var status=sheet.getRange(“F:F”).getValues();
var到期日=sheet.getRange(“M:M”).getValues();
var potential_capitaloutlay_USD=sheet.getRange(“Z:Z”).getValues();
Logger.log(“状态长度=“+status.Length”);
Logger.log(“到期日长度=+到期日.Length”);
Logger.log(“潜在资本流出量的长度=+潜在资本流出量的长度”);
对于(变量i=0;i
function Put_Options_Expiry_Alert() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Long equity (sell puts)");
//var timeZone = AdsApp.currentAccount().getTimeZone(); //Get timezone of current spreadsheet
var status = sheet.getRange("F:F").getValues();
var expiry_date = sheet.getRange("M:M").getValues();
var potential_capitaloutlay_USD = sheet.getRange("Z:Z").getValues();
Logger.log("Length of status = " + status.length);
Logger.log("Length of expiry_date = " + expiry_date.length);
Logger.log("Length of potential_capitaloutlay_USD = " + potential_capitaloutlay_USD.length);
for (var i = 0; i < status.length; i++) {
if (status[i] == "Entered") { //Evaluate if this is a live Put position
//Calculate the time difference of two dates using date2. getTime() – date1. getTime();
//Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day (1000*60*60*24)
Logger.log("expiry date is = "+expiry_date[i]);
Logger.log("today's date is = "+Date());
var days_to_expiry = dateDiffInDays(expiry_date[i],Date());
Logger.log(days_to_expiry);
}
}
}
// Function that returns the number of days difference between DateA and DateB
// DateA and DateB are javascript Date objects
function dateDiffInDays(DateA, DateB) {
var milliseconds_per_day = 1000 * 24 * 60; // number of milliseconds in a day
const utcA = Date.UTC(2021, DateA.getMonth(), DateA.getDate());
const utcB = Date.UTC(2020, DateB.getMonth(), DateB.getDate());
return Math.floor((utc2 - utc1) / milliseconds_per_day);
}
function timeDiffDays(Start, End) {
var day = 86400000;
var t1 = new Date(Start).valueOf();
var t2 = new Date(End).valueOf();
var d = t2 - t1;
return Math.floor(d / day);
}