简体   繁体   English

Google Sheet Script - 日期差异 - 当前日期() - 电子表格中的日期()

[英]Google Sheet Script - Date Difference - Current Date() - Date() in spreadsheet

I`m trying to calculate dates in my data - NOW() (Current Date) in days我正在尝试计算数据中的日期 - NOW()(当前日期)以天为单位

So所以

Dates           Today              Days difference

2019-01-01     2019-03-11            X
2019-02-01     2019-03-11            X 

and I would like that Today to be updated everyday我希望今天每天更新

Is there way to do this in Script so that "Days difference" can be updated daily automatically?有没有办法在脚本中做到这一点,以便“天差”可以每天自动更新?

Also is it possible to highlight any rows with red that are greater than 100 days?是否可以用红色突出显示超过 100 天的任何行?

Googlescripts seem to use dates that are basically the same as javascript dates, so you should be able to do something like this: 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);

I can't say for sure that googlescripts supports Math.floor() and the like, but this should get you close.我不能肯定 googlescripts 支持 Math.floor() 等,但这应该会让你接近。

Assuming the automatic formatting of .toLocaleString isn't what you want, you can use the .getFullYear , .getMonth , and .getDate methods (and pad any single-digit days with a leading zero), then concatenate the results into the YYYY-MM-DD format.假设.toLocaleString的自动格式化不是您想要的,您可以使用.getFullYear.getMonth.getDate方法(并用前导零填充任何一位数的天数),然后将结果连接到YYYY-MM-DD格式。 (Note that January is month number zero when converting month numbers to text.) (请注意,将月份数字转换为文本时,一月是月份数字零。)

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date and请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
https://developers.google.com/google-ads/scripts/docs/features/dates for more info. https://developers.google.com/google-ads/scripts/docs/features/dates了解更多信息。

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

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