我试图计算我的数据中的日期 - 现在()(当前日期)以天为单位
所以
Dates Today Days difference
2019-01-01 2019-03-11 X
2019-02-01 2019-03-11 X
我希望今天每天更新
有没有办法在脚本中执行此操作,以便“天差”可以每天自动更新?
还可以突出显示任何大于100天的红色行吗?
Googlescripts似乎使用与javascript日期基本相同的日期,因此您应该可以执行以下操作:
var dateFromFirstColumn = new Date("2019-01-01");
var now = new Date();
var today = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
0,0,0); // Midnight last night, since presumably the first date is similar
var todayString = today.toLocaleString(); // Can be written to second column
var diff = today.getTime() - dateFromFirstColumn.getTime();
var millisecondsInADay = 1000 * 60 * 60 * 24;
var diffInDays = Math.floor(diff/millisecondsInADay);
console.log(diffInDays);
我不能肯定googlescripts支持Math.floor()等,但这应该让你接近。
假设.toLocaleString
的自动格式化不是你想要的,你可以使用.getFullYear
,.getMonth
和.getDate
方法(并填充前导零的任何一位数天),然后将结果连接成YYYY-MM-DD
格式。 (请注意,将月份数转换为文本时,1月是月号为零。)
见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date和 https://developers.google.com/google-ads/scripts/docs/features/dates了解更多信息。