Javascript日期对象和date.prototype定制

Javascript日期对象和date.prototype定制,javascript,jquery,date,Javascript,Jquery,Date,我使用的是一个日历插件,该插件有一些回调事件,允许您自定义用户单击日期等时发生的情况。对我来说,设置该插件的一种方法是: onDayClick: function(e) { window.location.href = 'http://www.testdomain.com/events/day/' + e.data.date; } .date是一个so,例如,如果单击,将返回: http://www.testdomain.com/events/day/Thu 2012年6月2014年2

我使用的是一个日历插件,该插件有一些回调事件,允许您自定义用户单击日期等时发生的情况。对我来说,设置该插件的一种方法是:

onDayClick: function(e) {
    window.location.href = 'http://www.testdomain.com/events/day/' + e.data.date;
}
.date
是一个so,例如,如果单击,将返回:

http://www.testdomain.com/events/day/Thu 2012年6月2014年2000:00:00 GMT+0100(英国夏令时)

我需要的是以下各项的预期输出:

http://www.testdomain.com/events/day/2014/07/17/
看一看日期对象文档,我觉得这相当容易

Date.prototype.GetCustomFormat = function() {
    return this.getFullYear()+'/'+getInTwoDigitFormat(this.getMonth())+'/'+getInTwoDigitFormat(this.getDate());
};
function getInTwoDigitFormat(val) {
    return val < 10 ? '0' + val : val;
}

onDayClick: function(e) {
    window.location.href = 'http://www.testdomain.com/events/day/' + e.data.date.GetCustomFormat();
}
这似乎现在起作用了。然而,如果我有一个事件在1号登陆。。。它使用上个月的第一个。所以,如果我点击7月1日,它会返回6月1日

我想我抓住了它。。。但是这里那里有一些奇怪的结果。谁能指出我哪里做错了,把我纠正过来


感谢

对此的解释很简单:因为您比UTC早一个小时,在7月1日午夜,UTC仍然在6月份!这就是为什么它使用UTC月份输出6月1日。使用UTC月份而不是UTC年份和日期没有多大意义,因此,只需使用常规月份:

Date.prototype.GetCustomFormat = function() {
    return this.getFullYear()+'/'+getInTwoDigitFormat(this.getMonth()+1)+'/'+getInTwoDigitFormat(this.getDate());
};
var testerDate = new Date(Date.parse("Thu Jun 1 2012 00:00:00 GMT+0100"))
testerDate.GetCustomFormat() //The output of this depends on your time zone: It'll be 2012/06/01 if in or ahead of GMT+0100, but otherwise, it'll be 2012/05/31.

奇怪的是,它也带来了错误的一天。该月预计为-1,因为它的索引为零。因为它是UTC月,假设您在GMT+0100的7月1日午夜,UTC月会少一个不是很正常吗?您知道。。。我肯定我本来就有这个,但没用。但遗憾的是:谢谢你的重申。
Date.prototype.GetCustomFormat = function() {
    return this.getFullYear()+'/'+getInTwoDigitFormat(this.getMonth()+1)+'/'+getInTwoDigitFormat(this.getDate());
};
var testerDate = new Date(Date.parse("Thu Jun 1 2012 00:00:00 GMT+0100"))
testerDate.GetCustomFormat() //The output of this depends on your time zone: It'll be 2012/06/01 if in or ahead of GMT+0100, but otherwise, it'll be 2012/05/31.