计算Google Apps脚本中的年,月,日之间的日期 [英] Calculating year, month, days between dates in google apps script

查看:141
本文介绍了计算Google Apps脚本中的年,月,日之间的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下函数进行计算,它以X YEARS,Y MONTHS,Z DAYS的格式给出o / p,并在某些日期给出错误的o / p。我想我在公式中缺少一些计算。



功能是,

  / ** 
* @param {Date} startdate
* @param {Date} enddate
* @return {String}
* /
函数leasePeriodCalc(startDate,endDate)
{
var sdate = startDate;
var edate = endDate;
edate.setDate(edate.getDate()+ 1);
edate = new Date(edate);
if(sdate.valueOf()> edate.valueOf()){
return('0');
}
else {
var years =((((edate.getDate() - sdate.getDate())< 0?-1:0)+((edate.getMonth )+1) - (sdate.getMonth()+ 1)))< 0?-1:0)+(edate.getFullYear() - sdate.getFullYear());
var months =((((edate.getDate() - sdate.getDate())< 0?-1:0)+((edate.getMonth()+ 1) - (sdate.getMonth +1)))< 0〜12:0)+((edate.getDate() - sdate.getDate())< 0〜-1:0)+((edate.getMonth()+ 1) - ( sdate.getMonth()+ 1));
if((edate.getMonth() - 1)!= 1.0)
{
var days =((edate.getDate() - sdate.getDate())< 0?new Date(edate.getFullYear(),edate.getMonth(),0).getDate():0)+(edate.getDate() - sdate.getDate());
}
else
{
var days =((edate.getDate() - sdate.getDate())< 0?new Date(edate.getFullYear(),edate .getMonth()+ 1,0).getDate():0)+(edate.getDate() - sdate.getDate());
}
var day;
var month;
var year;
if(years> 1)year = years +'Years';
else year = years +'Year';
if(months> 1)month = months +'Months';
else month = months +'Month';
if(days> 1)day = days +'Days';
else day = days +'Day';
if(years == 0&& month!= 0&& days!= 0)return(month +','+ day);
else if(years!= 0&&& amp; month == 0&& days!= 0)return(year +','+ day);
else if(years!= 0&& month!= 0&&& days == 0)return(year +','+ month);
else if(years == 0&& month == 0&& days!= 0)return(day);
else if(years == 0&& months!= 0&&& days == 0)return(month);
else if(years!= 0& amp;& month == 0&& days == 0)return(year);
else if(years == 0&& month == 0& amp;& days == 0)return(day);
else if(years!= 0&&& month!= 0&&& days!= 0)return(year +','+ month +','+ day)
}
}

如果您给出下面的i / p返回假的o / p:



28th feb 2013 - 28th feb 2014



预计o / p:1年,1天



给定o / p:1年,4天



但是如果我选择28th feb 2013 - 27th feb 2014意味着,它给出了正确的o / p:



预期o / p:1 YEAR





如果我做了任何事情,请提供更正我的错误的建议。



我也不得不告诉我,我没有设置所有的规则。一般来说,一个月正在按照当月的日子进行计算。



例如,如果我们从银行获得贷款,我们将每月支付利息,即使该月份可能有30天或29天或28天或31天。



如果我们每月租一个房间,我们每月只会交租金?即使是4月20日 - 4月19日。即使它包含31天,据说只有一个月。请帮助我得出结论。



Tnx,
CL。

解决方案

为了在JavaScript中进行复杂的日期/时间操作,我发现 Moment.js 库真的有帮助。我将其包装到Apps脚本库中,您可以使用项目键 MHMchiX6c1bwSqGM1PZiW_PxhMjh3Sh48 我使用它来解决这个问题,虽然我可能错过了一些边缘案例。

  //加载库键:MHMchiX6c1bwSqGM1PZiW_PxhMjh3Sh48)。 
var moment = Moment.load();

函数getDuration(startDate,endDate){
var start = moment(startDate);
var end = moment(endDate);
var units = ['years','months','days'];
var parts = [];
units.forEach(function(unit,i){
var diff = Math.floor(end.diff(start,unit,true));
if(diff> 0 || i == units.length - 1){
end.subtract(unit,diff);
parts.push(diff +''+ unit);
}
})
return parts.join(',');
}


I have calculated using the below function and it gives the o/p in the format of "X YEARS, Y MONTHS, Z DAYS" and for some dates its giving some wrong o/p. I think I did some calculation missing in the formulas.

The function is,

/**
 * @param {Date} startdate
 * @param {Date} enddate
 * @return {String}
 */
function leasePeriodCalc(startDate,endDate)
{
  var sdate=startDate;
  var edate=endDate;
  edate.setDate( edate.getDate()+1);
  edate=new Date(edate);
  if(sdate.valueOf()>edate.valueOf()){
    return('0');
  }
  else{
    var years=((((edate.getDate()-sdate.getDate())<0 ? -1:0)+((edate.getMonth()+1)-(sdate.getMonth()+1)))< 0 ? -1 : 0)+(edate.getFullYear()-sdate.getFullYear());
    var months=((((edate.getDate()-sdate.getDate())<0 ? -1:0)+((edate.getMonth()+1)-(sdate.getMonth()+1)))< 0 ?12:0)+((edate.getDate()-sdate.getDate())<0 ? -1:0)+((edate.getMonth()+1)-(sdate.getMonth()+1));
    if((edate.getMonth()-1)!=1.0)
    {
      var days=((edate.getDate()-sdate.getDate())< 0 ?new Date(edate.getFullYear(), edate.getMonth(),0).getDate():0)+(edate.getDate()-sdate.getDate());
    }
    else
    {
      var days=((edate.getDate()-sdate.getDate())< 0 ?new Date(edate.getFullYear(), edate.getMonth()+1,0).getDate():0)+(edate.getDate()-sdate.getDate());
    }
    var day;
    var month;
    var year;
    if(years>1)year= years+ 'Years';
    else year=years+'Year';
    if(months>1) month= months+ 'Months';
    else month=months+'Month';
    if(days>1) day= days+ 'Days';
    else day=days+'Day';
    if(years==0&&months!=0&&days!=0) return(month+', '+day);
    else if(years!=0&&months==0&&days!=0) return(year+', '+day);
    else if(years!=0&&months!=0&&days==0) return(year+', '+month);
    else if(years==0&&months==0&&days!=0) return(day);
    else if(years==0&&months!=0&&days==0) return(month);
    else if(years!=0&&months==0&&days==0) return(year);
    else if(years==0&&months==0&&days==0) return(day);
    else if(years!=0&&months!=0&&days!=0) return(year+', '+month+', '+day);
  }
}

if you gives the i/p as below it returning the false o/p:

28th feb 2013 - 28th feb 2014

Expected o/p : 1 YEAR , 1 DAY

Given o/p : 1 YEAR , 4 DAYS

But if I select 28th feb 2013 - 27th feb 2014 means, It gave the correct o/p:

Expected o/p : 1 YEAR

Given o/p : 1 YEAR

Please advice to correct my fault if I did anything.

And also I have to tell that I'm not setting the rules n all. In general a month is calculating as per the days lying on the month.

For example, If we get a loan from a bank we ll pay the interest per month only even that month may have 30 days or 29 days or 28 days or 31 days.

And also if we take a room for monthly rental means, We ll pay the rent per month only rite? even it can be from 20th March - 19th April. Even it contains 31 days it is said to be one month only. Please help me to conclude this.

Tnx, CL.

解决方案

For complex date/time manipulations in JavaScript I find that the Moment.js library can really help. I wrapped it up into an Apps Script library, which you can include using the project key MHMchiX6c1bwSqGM1PZiW_PxhMjh3Sh48. I used it to take a crack at this problem, although I may have missed some edge cases.

// Load the library (key: MHMchiX6c1bwSqGM1PZiW_PxhMjh3Sh48).
var moment = Moment.load();

function getDuration(startDate, endDate) {
  var start = moment(startDate);
  var end = moment(endDate);
  var units = ['years', 'months', 'days'];
  var parts = [];
  units.forEach(function(unit, i) {
    var diff = Math.floor(end.diff(start, unit, true));
    if (diff > 0 || i == units.length - 1) {
      end.subtract(unit, diff);
      parts.push(diff + ' ' + unit);
    }
  })
  return parts.join(', ');
}

这篇关于计算Google Apps脚本中的年,月,日之间的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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