javascript - 为什么我在 Angular2+ 中尝试填充自定义日历时会在 11 月得到两个日期/时间
问题描述
我的任务是创建一个日历。在我的代码中,我有获取当前、下个月和上个月的方法。在我的 setDates 方法中,您可以看到我将月初的前一个星期日作为一周中的一天开始。然后,我将日历填充 6 周 (6 x 7),总共 42 天。
例如,每当我进入 11 月份时,它都会显示两个 11 月 1 日。当我安慰这几天的事情时,我得到了这个:
0: Sun Nov 01 2020 00:32:54 GMT-0500 (Central Daylight Time) {}
1: Sun Nov 01 2020 23:32:54 GMT-0600 (Central Standard Time) {}
2: Mon Nov 02 2020 23:32:54 GMT-0600 (Central Standard Time) {}
3: Tue Nov 03 2020 23:32:54 GMT-0600 (Central Standard Time) {}
4: Wed Nov 04 2020 23:32:54 GMT-0600 (Central Standard Time) {}
5: Thu Nov 05 2020 23:32:54 GMT-0600 (Central Standard Time) {}
6: Fri Nov 06 2020 23:32:54 GMT-0600 (Central Standard Time) {}
看起来那是夏令时的时候。所以在我的日历上,它填充了两天,显示 11 月 1 日,然后移动到 11 月 2 日,这不是我想要的。我该如何解决这个问题以考虑夏令时?我是否需要创建一个方法并以某种方式检查日期是否是夏令时?
我的 .html 文件
<div class="row">
<div class="col-12">
<table class='table table-striped table-narrow'>
<tr class='month'>
<td >
<fa-icon style="padding-left: 30px;" [icon]="faAngleDoubleLeft" class='month_prev'(click)="previousMonth()"></fa-icon>
</td>
<td>
{{ currMonth() }}
</td>
<td>
<fa-icon style="padding-right: 30px;" [icon]="faAngleDoubleRight" class ='month_next'(click)="nextMonth()"></fa-icon>
</td>
</tr>
</table>
</div>
</div>
<div class="row">
<div class="col-12">
<table class='table table-bordered table-fixed-header table-striped table-narrow'>
<tr class='weekdays'>
<td>
Sunday
</td>
<td>
Monday
</td>
<td>
Tuesday
</td>
<td>
Wednesday
</td>
<td>
Thursday
</td>
<td>
Friday
</td>
<td>
Saturday
</td>
</tr>
<tr *ngFor="let w of allDates">
<td class='days' (click)="showTimeEntries(d)" *ngFor="let d of w">
{{d.getDate()}}
</td>
</tr>
</table>
</div>
</div>
.ts 文件
export class CalendarHoursComponent implements OnInit, OnDestroy {
faAngleDoubleLeft=faAngleDoubleLeft;faAngleDoubleRight=faAngleDoubleRight; allDates = []; allMonths = [];
resources: IResource[]=[]; resourceId: IResource = null;
currentDate: Date = new Date();
month = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
constructor(private server: ServerService, private globals: GlobalsService, private modalService: NgbModal) { }
ngOnInit() {
this.setDates();
}
ngOnDestroy() {
try { this.modalService.dismissAll(); } catch(err) { console.log(err); }
}
showTimeEntries(d: Date) {
CalendarTimeEntriesComponent.show(this.modalService, this.globals.currentUser.id, d).then(r => {}).catch(r => {});
}
setDates() {
let firstSunday = this.currentDate;
while (firstSunday.getDate() != 1) {
firstSunday = new Date(firstSunday.getTime() - (24 * 60 * 60 * 1000));
}
while (firstSunday.getDay() != 0) {
firstSunday = new Date(firstSunday.getTime() - (24 * 60 * 60 * 1000));
}
let temp=[];
for (let i = 0; i < 42; i++) {
let day = new Date(firstSunday.getTime() + (24 * 60 * 60 * 1000) * i);
temp.push(day);
if (temp.length%7==0)
{
this.allDates.push(temp);
temp=[];
}
}
console.log(this.allDates);
}
addMonths(date: Date, months: number) {
let d = date.getDate();
date.setMonth(date.getMonth() + months);
if (date.getDate() != d) {
date.setDate(0);
}
return date;
}
currMonth() {
return this.month[this.currentDate.getMonth()];
}
nextMonth() {
this.currentDate = this.addMonths(this.currentDate, 1);
this.allDates = [];
this.setDates();
}
previousMonth() {
this.currentDate = this.addMonths(this.currentDate, -1);
this.allDates = [];
this.setDates();
}
}
解决方案
推荐阅读
- excel - Excel 2016 向 LAST ROW 插入新表行弄乱了公式
- swift - 创建 plist 并将其复制到文件夹 MacOS Swift
- c - 如何选择其中一个选项?
- php - 如何在 Laravel 5.6 的数据依赖选择输入中获取表数据以编辑表单?
- java - 错误:找不到符号(resultSetToTableModel)
- ubuntu - 运行“netplan apply”命令后,Netplan 和 Ubuntu 18.04 关闭我们的网络的问题
- javascript - 转置 d3.nest 结果的值
- mysql - 在 MySQL 查询中包含不在“BETWEEN”范围内的日期
- java - IntelliJ IDEA Sprig MVC Tomcat 在 mac 上设置 /conf 文件
- java - 访问应用程序外部文件夹文件以在播放框架2.6.x的模板中呈现的方式是什么?