gpt4 book ai didi

Javascript 新日期函数为两个相似的函数调用生成不同的小时值

转载 作者:行者123 更新时间:2023-11-29 21:00:39 27 4
gpt4 key购买 nike

<分区>

我有一个 angular.js 应用程序,我想获取当周的星期一和星期日的日期,然后将它们默认设置为日历日期。由于生产日期不在 7 天的时间跨度内,我看到了一个错误。我解决了这个问题。

但是当我研究代码时我发现,类似的日期创建代码会产生不同的小时值。这是我的代码片段:

var current = new Date(); // get current date    
var weekstart = current.getDate() - current.getDay() + 1;
var weekend = weekstart + 6; // end day is the first day + 6
var firstday = new Date(current.setDate(weekstart));
firstday = new Date(firstday.getFullYear() + '-' + (firstday.getMonth() + 1)
+ '-' + firstday.getDate());
console.log("Monday: " + firstday);

//firstday.setHours(0, 0, 0, 0);
var last = new Date(firstday);
last.setDate(last.getDate() + 6);
console.log("Sunday 1: " + last);
var lastday = new Date(last.getFullYear() + '-' + (last.getMonth() + 1)
+ '-' + last.getDate());
console.log("Sunday 2: " + lastday);

这是在我的控制台中生成的值:

Monday: Mon Oct 09 2017 00:00:00 GMT+1100 (AUS Eastern Daylight Time)
Sunday 1: Sun Oct 15 2017 00:00:00 GMT+1100 (AUS Eastern Daylight Time)
Sunday 2: Sun Oct 15 2017 11:00:00 GMT+1100 (AUS Eastern Daylight Time)

为什么星期日 2 中的 Hour 字段生成为 11,而星期一的类似新 Date 调用结果为 00?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com