当月第二个星期一的Javascript日期
Javascript Date for the Second Monday of the month
我正在与一个小组合作,该小组在本月的第二个星期一开会,他们希望他们的网站反映下一次会议的日期。我有本月第二个星期一放映的剧本,但我对if-else语句有意见。我需要它来反映下一个即将到来的事件,而不仅仅是这个月的日期。IE。本月活动日期为2012年8月13日,已超过当前日期(2012年7月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
*/
x
函数缺少()
,所以它没有执行它。:)应该是:
if (Date.prototype.x() > todayDate)
更新:这里有一个已清理的逻辑的固定/工作版本(可能有过多的评论,但我想如果有人需要的话,它至少在那里)。
Date.prototype.nextSecondMonday = function (){
// Load the month.
var target = new Date(this.getFullYear(), this.getMonth(), 1, 0, 0, 0);
var today = new Date();
// Check to see if the 1st is on a Monday.
var isMonday = (target.getDay() == 1);
// Jump ahead two weeks from the 1st, and move back the appropriate number of days to reach the preceding Monday.
// i.e. If the 1st is a Thursday, we would move back three days.
var targetDate = 15 - (target.getDay() - 1);
// Quick adjustment if the 1st is a Monday.
if (isMonday) targetDate -= 7;
// Move to the second Monday in the month.
target.setDate(targetDate);
// Second Monday is before today's date, so find the second Monday next month.
if (today > target) {
//return "<em>" + target.toLocaleDateString() + " is in the past...</em>";
target.setMonth(target.getMonth() + 1);
return target.nextSecondMonday();
}
// Format and return string date of second Monday.
return target.toLocaleDateString();
}
// Working test for the year 2012.
//for (var i = 0; i < 12; i++)
//$("#log").append(new Date(2012, i).nextSecondMonday() + "<br /><br />");
相关文章:
- Javascript日期格式类似于ISO,但本地
- 正在搜索JavaScript日期选择器滑块
- 将Javascript日期转换为ASP.NET日期格式
- Javascript日期修改
- 转换异常时间&Datestring到JavaScript日期
- 从int值来看,Javascript日期不正确
- Chrome Javascript日期构造函数错误
- Javascript日期解析
- javascript日期减去小时-错误
- PHP strtotime()为JavaScript日期字符串返回false
- 复制Javascript日期选择器
- javascript日期库重复十月
- 正在使用日期字符串初始化javascript日期对象
- 正在设置未来的Javascript日期
- 来自控制器的javascript日期
- asp-netc#DateTime转换为javascript日期
- 使用JavaScript日期的任何时间的时区偏移量
- javascript日期时区问题
- JavaScript日期&时间转换tz到正常
- 当天数大于月份的天数时,javascript日期会创建奇怪的日期