本月第二个星期一的Javascript日期 [英] Javascript Date for the Second Monday of the month

查看:137
本文介绍了本月第二个星期一的Javascript日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和一个符合本月第二个星期一的小组合作,他们希望他们的网站能够反映NEXT的会议日期。我有脚本显示这个月的第二个星期一,但我有麻烦的if else语句。我需要它反映下一个即将到来的活动,而不仅仅是这个月份的日子。 IE。这个月的事件日期是2012年8月13日,超过了当前日期(2012年8月21日)。我想要转到2012年9月10日的下一个可用日期。以下是我到目前为止的代码。

I am working with a group that meets the second monday of the month and they want their site to reflect the NEXT meeting date. I have the script to show this months second monday, but i am having trouble with the if else statement. I need it to reflect the next upcoming event and not just this months date. IE. this months event date was Aug 13 2012 which is past the current date (aug 21 2012). I would like it to move to the next available date Sept 10 2012. Below is the code i have so far.

<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>


推荐答案

如果当月的第二个星期一的日期小于当前日期,
在下个月的第一个月内调用该函数。

If the date of the second Monday of the current month is less than the current date, call the function on the first of the next month.

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日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆