Angular 如何设置与开始日期不同的结束日期?

Angular 如何设置与开始日期不同的结束日期?,angular,date,Angular,Date,我已经创建了一个日历,我需要在其中显示用户每天工作的总小时数。 下面是它的外观: 无论H显示在何处,都会显示小时数 我创建了一个名为getTimeEntrysForDateRange的方法,其中的参数是:resourceID(它告诉我当前登录的用户)、startDay、startMonth、startYear、lastDay、lastMonth、lastYear 我能够得到正确的前4个参数,例如,如果我在9月份,startDay、startMonth和startYear是2020年8月30日。

我已经创建了一个日历,我需要在其中显示用户每天工作的总小时数。 下面是它的外观:

无论H显示在何处,都会显示小时数

我创建了一个名为getTimeEntrysForDateRange的方法,其中的参数是:resourceID(它告诉我当前登录的用户)、startDay、startMonth、startYear、lastDay、lastMonth、lastYear

我能够得到正确的前4个参数,例如,如果我在9月份,startDay、startMonth和startYear是2020年8月30日。我从该日期开始的原因在代码中很明显,在日历上也很明显(一周的开始时间是星期天),因此我创建了一个名为setDates()的方法,该方法将从上一个星期天到月初

当我在“网络”选项卡中检查通话时,它会将开始日期和结束日期显示为2020年8月30日。我希望开始日期是日历上显示的第一个星期日,然后我希望结束日期是(在本例中)10月3日的最后一个星期六

注意:我每月只显示35天或5周

我的ts代码

导出类CalendarHoursComponent实现OnInit、OnDestroy{
faAngleDoubleLeft=faAngleDoubleLeft;
faAngleDoubleRight=faAngleDoubleRight;
所有日期=[];
所有月份=[];
资源:IResource[]=[];
currentDate:日期=新日期();
小时数:ITIMementry=null;
第一个星期日:日期;
月份=[
“一月”,
“二月”,
“三月”,
“四月”,
“五月”,
“六月”,
“七月”,
“八月”,
“9月”,
“十月”,
“11月”,
“12月”,
];
建造师(
专用服务器:服务器服务,
专用全球服务:全球服务,
专用modalService:NgbModal
) {}
恩戈尼尼特(){
这个.setDates();
这个.load();
}
恩贡德斯特罗(){
试一试{
this.modalService.dismissAll();
}捕捉(错误){
控制台日志(err);
}
}
加载(){
让lastSaturday=this.firstSunday;
lastSaturday.setDate(lastSaturday.getDate()+31);
调试器;
this.server.getTimeEntrysForDateRange(
this.globals.currentUser.id,
此.firstSunday.getMonth(),
this.firstSunday.getDate(),
这个。第一个星期天。getFullYear(),
上周六。getMonth(),
lastSaturday.getDate(),
上周六。getFullYear(),
(r) =>此.load2(r)
);
}
私有加载2(d:any[]){}
showTimeEntries(d:日期){
CalendarTimeEntriesComponent.show(
这个.modalService,
this.globals.currentUser.id,
D
)
.然后((r)=>{})
.catch((r)=>{});
}
设置日期(){
this.firstSunday=this.currentDate;
while(this.firstSunday.getDate()!=1){
this.firstSunday=新日期(
this.firstSunday.getTime()-24*60*60*1000
);
}
而(this.firstSunday.getDay()!=0){
this.firstSunday=新日期(
this.firstSunday.getTime()-24*60*60*1000
);
}
设temp=[];
for(设i=0;i<35;i++){
let day=新日期(this.firstSunday.getTime()+24*60*60*1000*i);
温度推送(天);
如果(临时长度%7==0){
此.allDates.push(临时);
温度=[];
}
}
console.log(this.allDates);
}
添加月份(日期:日期,月份:编号){
设d=date.getDate();
date.setMonth(date.getMonth()+月);
if(date.getDate()!=d){
日期。设置日期(0);
}
返回日期;
}
货币月份(){
返回this.month[this.currentDate.getMonth()];
}
下个月{
this.currentDate=this.addMonths(this.currentDate,1);
this.allDates=[];
这个.setDates();
}
上月{
this.currentDate=this.addMonths(this.currentDate,-1);
this.allDates=[];
这个.setDates();
}
}
我不认为这是相关的,但要全面,这里是html代码


星期日
星期一
星期二
星期三
星期四
星期五
星期六
{{d.getDate()}
H

问题在于Javascript
Date
s是可变对象。在
load()
代码中,前几行是问题所在:

  load() {
    let lastSaturday = this.firstSunday;
    lastSaturday.setDate(lastSaturday.getDate() + 31);
    // ...
  }
它将局部变量
laststaday
设置为指向内存中与
this.firstSunday
相同的
Date
对象。因此,在下一行中,当您在其上调用
.setDate()
时,它会修改两个引用所指向的同一个对象

相反,您需要复制this.firstSunday的
值,但获取一个全新的
Date
对象。一种方法是将另一个日期对象的数值传递给
date
构造函数

以下是等效的:

const lastSaturday = new Date(+this.firstSunday);
lastSaturday.setDate(lastSaturday.getDate() + 31);

const lastSaturday = new Date(this.firstSunday.valueOf());
lastSaturday.setDate(lastSaturday.getDate() + 31);

我的GetTimeEntrysForDaterRange方法的第2-7个参数仍然是2020年8月30日。20年8月30日对于第二个到第四个参数是可以的,因为这些是我的开始日期,但对于第五个到第七个参数,我希望它们显示或从2020年8月30日或2020年9月30日起31天。