我正在与一个在每月第二个星期一开会的小组合作,他们希望他们的网站能够反射(reflect)下一次 session 的日期。我有本月第二个星期一显示的脚本,但我在使用 if else 语句时遇到了问题。我需要它来反射(reflect)下一个即将发生的事件,而不仅仅是本月的日期。 IE。本月的事件日期是 2012 年 8 月 13 日,已超过当前日期(2012 年 8 月 21 日)。我希望它移至下一个可用日期 2012 年 9 月 10 日。下面是我到目前为止的代码。
<script type="text/javascript">
Date.prototype.x = function () {
var d = new Date (this.getFullYear(), this.getMonth(), 1, 0, 0, 0)
d.setDate (d.getDate() + 15 - d.getDay())
return d
}
Date.prototype.getSecondMonday = function () {
var d = new Date (this.getFullYear(), 1, 1, 0, 0, 0)
d.setMonth(this.getMonth()+1)
d.setDate (d.getDate() + 15 - d.getDay())
return d
}
var today = new Date()
var todayDate = today.toDateString()
if (Date.prototype.x>todayDate)
{
document.write (new Date().x().toDateString());
}
else
{
document.write (new Date().getSecondMonday().toDateString());
}
</script>
最佳答案
如果当月第二个星期一的日期小于当前日期, 在下个月的第一天调用该函数。
Date.prototype.nextSecondMonday= function(){
var temp= new Date(this), d= temp.getDate(), n= 1;
while(temp.getDay()!= 1) temp.setDate(++n);
temp.setDate(n+7);
if(d>temp.getDate()){
temp.setMonth(temp.getMonth()+1, 1);
return temp.nextSecondMonday();
}
return temp.toLocaleDateString();
}
/* tests
var x= new Date(2012, 7, 22);
x.nextSecondMonday()
Monday, September 10, 2012
var x= new Date(2012, 7, 12);
x.nextSecondMonday()
Monday, August 13, 2012
*/
关于Javascript 日期为该月的第二个星期一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12065146/