[英]Calculating difference between two dates in Google Apps Script
I am getting datetime from an API call like following-: 我从API调用获取datetime,如下所示:
2017-11-21T20:23:26+0000 2017-11-21T20:23:26 + 0000
Now I want to compare this with today's date and calculate the difference in number of days, how can i do that in google apps script. 现在我想将此与今天的日期进行比较并计算天数的差异,我该如何在谷歌应用程序脚本中执行此操作。
thank you for your help. 谢谢您的帮助。
i think you can do something like this, 我想你可以做这样的事情,
var dt1 = new Date(), // today's date
dt2 = new Date("2017-11-21T20:23:26+0000"); // your date from API
// get milliseconds
var t1 = dt1.getTime(),
t2 = dt2.getTime();
var diffInDays = Math.floor((t1-t2)/(24*3600*1000));
// 24*3600*1000 is milliseconds in a day
console.log(diffInDays);
Here's days, minutes and hours with a different approach: 这是使用不同方法的日期,分钟和小时:
function getDaysHoursAndMinutes(){
var hd=new Date('2017-11-21T20:23:26+0000').valueOf();
var td=new Date().valueOf();
var sec=1000;
var min=60*sec;
var hour=60*min;
var day=24*hour;
var diff=td-hd;
var days=Math.floor(diff/day);
var hours=Math.floor(diff%day/hour);
var minutes=Math.floor(diff%day%hour/min);
Logger.log('%s days %s hours %s minutes',days,hours,minutes);
}
The basic idea is that the value of a date is the number of milliseconds from a given date time reference which I think in this case is January 1, 1970. 基本思想是日期的值是给定日期时间参考的毫秒数,我认为在这种情况下是1970年1月1日。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.