获取具有特定时间的Date()对象 [英] Get Date() object with specific time
问题描述
我想获得一周的开始日期.我能够得到日期,它只是返回的日期具有到当前系统时间的时间.例如,如果现在是19:20,则我将周开始日期设为日期{2012年3月26日星期一19:20:16 GMT + 0530(IST)}
I want to get week Start Date of a week. I am able to get the date, its just that the date returned has time wrt to the current system time. For example, if its 19:20 hours right now, I am getting the week start date as Date {Mon Mar 26 2012 19:20:16 GMT+0530 (IST)}
为了进行准确的计算,我需要将时间设为格林尼治标准时间00:00:00.
For accurate calculations, I need the time to be 00:00:00 GMT.
我该如何实现?
我当前的代码如下:
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当前正在使用当前时间戳发出错误的开始日期.
this.weekStartDate is currently handing out the false start date with the current timestamp.
推荐答案
您可以使用以下方法将日期设置为一周的开始:
You can set the date to the start of the week with this:
var d = new Date();
d.setDate(d.getDate() - d.getDay());
然后使用 .setHours(0)
, .setMinutes(0)
等来清除时间.
Then use .setHours(0)
, .setMinutes(0)
, etc. to clear out the time.
这篇关于获取具有特定时间的Date()对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!