Javascript 当我试图用Angular2+;填充自定义日历时,为什么会在11月得到两个日期/时间;
我的任务是创建日历。在我的代码中,我有获取当前、下一个月和上一个月的方法。在我设置日期的方法中,您可以看到我将月初的前一个星期日设置为一周的第一天。然后,我将填充日历6周(6 x 7),总共42天 例如,每当我进入11月,它都会显示两个11月1日。当我安慰这一系列的日子时,我发现:Javascript 当我试图用Angular2+;填充自定义日历时,为什么会在11月得到两个日期/时间;,javascript,angular,date,Javascript,Angular,Date,我的任务是创建日历。在我的代码中,我有获取当前、下一个月和上一个月的方法。在我设置日期的方法中,您可以看到我将月初的前一个星期日设置为一周的第一天。然后,我将填充日历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 (Centra
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>
{{currMonth()}}
星期日
星期一
星期二
星期三
星期四
星期五
星期六
{{d.getDate()}
.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();
}
}
导出类CalendarHoursComponent实现OnInit、OnDestroy{
faAngleDoubleLeft=faAngleDoubleLeft;faAngleDoubleRight=faAngleDoubleRight;所有日期=[];所有月份=[];
资源:IResource[]=[];资源ID:IResource=null;
currentDate:日期=新日期();
月份=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”];
构造函数(私有服务器:ServerService,私有全局:GlobalService,私有modalService:NgbModal){}
恩戈尼尼特(){
这个.setDates();
}
恩贡德斯特罗(){
尝试{this.modalService.dismissAll();}catch(err){console.log(err);}
}
showTimeEntries(d:日期){
CalendarTimeEntriesComponent.show(this.modalService,this.globals.currentUser.id,d);
}
设置日期(){
让firstSunday=this.currentDate;
while(firstSunday.getDate()!=1){
firstSunday=新日期(firstSunday.getTime()-(24*60*60*1000));
}
while(firstSunday.getDay()!=0){
firstSunday=新日期(firstSunday.getTime()-(24*60*60*1000));
}
设temp=[];
for(设i=0;i<42;i++){
let day=新日期(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();
}
}
经过进一步的调试和检查,我的代码实际上运行得很好。我做了另一个控制台日志,11月1日的两个日期已经不存在了,它很好地填充了这些日期
我担心如果我需要抽出时间,它是否会扰乱时间。但是我正在将这个.currentDate设置为new Date(),它可以获取任何时间点的当前时间。您可以只使用
firstSunday.setDate(firstSundary.getDate()-1)
而不使用毫秒数。那你就不用担心时间的变化了。