减去Google Apps脚本中的天数将无法反映当年的正确更改 [英] Subtract days in google apps script will not reflect correct changes at year

查看:87
本文介绍了减去Google Apps脚本中的天数将无法反映当年的正确更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据这篇文章中的解决方案减去google apps脚本中的天数: 尝试从定义的日期中减去5天日期-Google App脚本

I am trying to subtract days in google apps script following the solution from this post: Trying to subtract 5 days from a defined date - Google App Script

但是,如果我们从一年的第一天减去一天,则年份不减去1. 下面是我的代码:

However, the year does not deduct by 1 if we minus one day from the first day of the year. Below are my code:

var StartDate = new Date(ResponceDetails[C3StartDate]);
var StartYear = StartDate.getYear();
var StartMonth = StartDate.getMonth();
var StartDay = StartDate.getDate();
var tempEndDate = new Date(StartYear+1, StartMonth, StartDay);
Logger.log(StartDate);
Logger.log(tempEndDate);
var EndDate = Utilities.formatDate(new Date(tempEndDate.getTime()-(24*3600*1000)),"GMT+8","MM/dd/YYYY");
Logger.log(EndDate);

此代码的目的是计算1年后的用户到期日期(包括开始日期).因此它必须是一年加上一年减去一天.上面的代码还可以,还是有类似问题的人?

The purpose of this code is to calculate the due date of user after 1 year (including start day). So it has to be plus one year and minus by 1 day. Does the code above ok or anyone having a similar problem?

谢谢

推荐答案

解决了我的问题! 年份YYYY必须在Utilities.formatDate(...)

Solved my problem! the year YYYY has to be small letters yyyy in the Utilities.formatDate(...)

更新的代码:

var StartDate = new Date(ResponceDetails[C3StartDate]);
var StartYear = StartDate.getYear();
var StartMonth = StartDate.getMonth();
var StartDay = StartDate.getDate();
var EndDate = Utilities.formatDate(new Date(StartYear+1, StartMonth, StartDay-1), "GMT+8", "MM/dd/yyyy");

但是任何更好的方法都可以实现. 如果有人在日期格式方面有一个不错的文档或参考资料,则说明日期格式"的详细信息确实很有帮助.非常感谢!

but any better ways to do this is very appreciated. and if anyone have a good document or references on the date format explaining the details of Date Format is really helps. Many thanks!

这篇关于减去Google Apps脚本中的天数将无法反映当年的正确更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆