gpt4 book ai didi

javascript - 获取具有特定时间的 Date() 对象

转载 作者:搜寻专家 更新时间:2023-11-01 05:06:48 24 4
gpt4 key购买 nike

我想获得一周的开始日期。我能够获取日期,只是返回的日期具有当前系统时间的时间。例如,如果现在是 19:20,我得到的星期开始日期为 Date {Mon Mar 26 2012 19:20:16 GMT+0530 (IST)}

为了准确计算,我需要时间为格林威治标准时间 00:00:00。

我怎样才能做到这一点?

我现在的代码是这样的:

var tDate = new Date();
var wDate = new Date();

this.chosenDayIndex = this.currentDayIndex = tDate.getDay();

if (this.currentDate == '') {

this.currentDate = new Date();
var today = formatDate(tDate);
var diff = tDate.getDate() - this.currentDayIndex + (this.currentDayIndex == 0 ? -6:1);

this.weekStartDate = this.weekMonDate = new Date(wDate.setDate(diff));
this.weekEndDate = new Date(this.weekStartDate.getFullYear(),this.weekStartDate.getMonth(), this.weekStartDate.getDate()+6);
this.weekMonday = formatDate(this.weekMonDate);

}

this.weekStartDate 当前正在分发带有当前时间戳的错误开始日期。

最佳答案

您可以将日期设置为一周的开始:

var d = new Date();
d.setDate(d.getDate() - d.getDay());

然后使用.setHours(0).setMinutes(0)等方法清空时间。

关于javascript - 获取具有特定时间的 Date() 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9944379/

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