廢話少說只就上Code:

說明:getDay()方法獲取星期(這里的星期是從0到6)。參見:http://www.w3school.com.cn/js/js_obj_date.asp 中的getDay().
代碼有不足之處希望得到指正。
var SecondFriday = { getSecondFriday: function () { var flag = 0; //(1) 獲取當月月初時間,時間格式為:Sun Jun 01 2014 00:00:00 GMT+0800 (中國標准時間) var thisDate = new Date(new Date().getFullYear(), new Date().getMonth()); for (var i = 0; i < 15; i++) { //(2) 因為是每個月的第二個星期五,所以循環15次以內就可以 wd = thisDate.getDay();//(3)判定當月月初是星期幾 if(wd===0){//(4)特殊處理星期天。 因為星期天為‘0’。 console.log('here'); return thisDate.getFullYear() + "-" + (thisDate.getMonth() + 1) + "-" +"13" } if ((i + wd) % 5 == 0) { flag = flag + 1;//(5)標示符,確定是第二個星期五 if (flag == 2) { return thisDate.getFullYear() + "-" + (thisDate.getMonth() + 1) + "-" + (i + wd) } } } } };
//Chrom 測試
console.log(SecondFriday.getSecondFriday());
@背塔者 ,提供的簡潔代碼:
版本一:
function getSecondFriday() { var now = new Date(); var first = new Date(now.getFullYear(), now.getMonth(), 1); now.setDate((5 - first.getDay()) + 7 + 1); // 星期五-第一天的的星期幾+1天+7天 return now; }
版本二:
// 星期五-第一天的的星期幾+1天+7天,星期六特殊處理了下 function getSecondFriday() { var now = new Date(); var first = new Date(now.getFullYear(), now.getMonth(), 1); now.setDate((5 - (first.getDay() > 5? -1: first.getDay())) + 7 + 1); return now; }
holine 總結:
function secondFriday(date){ date = new Date(date); date.setDate(1); date.setDate((12 - date.getDay()) % 7 + 8); return date; }