2017-07-03 85 views
1

我一直有問題搞清楚日期功能獲取所有星期一在今年

var d = new Date(), 
    month = d.getMonth(), 
    mondays = []; 

d.setDate(1); 

// Get the first Monday in the month 
while (d.getDay() !== 1) { 
    d.setDate(d.getDate() + 1); 
} 

// Get all the other Mondays in the month 
while (d.getMonth() === month) { 
    var pushDate = new Date(d.getTime()); 
    mondays.push(pushDate.getDate() + '-' + (pushDate.getMonth()+1) + '-' + pushDate.getFullYear()); 
    d.setDate(d.getDate() + 7); 
} 

我使用這個功能,目前每月能拿到一個月所有星期一。

我該如何修改此代碼以獲得今年剩餘的所有星期一?

+0

在最後循環的末尾:'如果(d.getMonth()=== 0)斷裂;' –

+0

約什麼'而(d.getMonth()== =月){'我不需要更新它嗎? –

+1

只需更改每月引用當月的內容即可查看年份。 – Barmar

回答

2

通過一年而不是每月只需循環。代碼與你的一樣,它工作正常。剛換月 - >年,得到月() - >得到年()

var d = new Date(), 
    year = d.getYear(), 
    mondays = []; 

d.setDate(1); 

// Get the first Monday in the month 
while (d.getDay() !== 1) { 
    d.setDate(d.getDate() + 1); 
} 

// Get all the other Mondays in the month 
while (d.getYear() === year) { 
    var pushDate = new Date(d.getTime()); 
    mondays.push(pushDate.getDate() + '-' + (pushDate.getMonth()+1) + '-' + pushDate.getFullYear()); 
    d.setDate(d.getDate() + 7); 
} 
0

var x = new Date(); 
 
//set the financial year starting date 
 
x.setFullYear(2016, 03, 01); 
 
//set the next financial year starting date 
 
var y = new Date(); 
 
y.setFullYear(2017, 03, 01); 
 
var j = 1; 
 
var count = 0; 
 
//getting the all mondays in a financial year 
 
for (var i = 0; x < y; i += j) { 
 
    if (x.getDay() === 1) { 
 
    document.write("Date : " + x.getDate() + "/" + 
 
     (x.getMonth() + 1) + "<br>"); 
 
    x = new Date(x.getTime() + (7 * 24 * 60 * 60 * 1000)); 
 
    j = 7; 
 
    count++; 
 
    } else { 
 
    j = 1; 
 
    x = new Date(x.getTime() + (24 * 60 * 60 * 1000)); 
 
    } 
 
} 
 
document.write("total mondays : " + count + "<br>");

+0

請不要通過添加24小時來添加日子。在觀察夏令時的地方,並非所有日子都是24小時。請參閱[*爲當前日期*添加+1](https://stackoverflow.com/questions/9989382/add-1-to-current-date)。 – RobG

+0

@RobG繼續前進並編輯。 – Rick

0

這只是一種選擇,它採用獲得本月的第一天更爲簡單的方法。

// Get all Mondays in year from provided date 
 
// Default today's date 
 
function getMondays(d) { 
 
    // Copy d if provided 
 
    d = d ? new Date(+d) : new Date(); 
 
    // Set to start of month 
 
    d.setDate(1); 
 
    // Store end year and month 
 
    var endYear = d.getFullYear() + 1; 
 
    var endMonth = d.getMonth(); 
 

 
    // Set to first Monday 
 
    d.setDate(d.getDate() + (8 - (d.getDay() || 7)) % 7); 
 
    var mondays = [new Date(+d)]; 
 

 
    // Create Dates for all Mondays up to end year and month 
 
    while (d.getFullYear() < endYear || d.getMonth() != endMonth) { 
 
    mondays.push(new Date(d.setDate(d.getDate() + 7))); 
 
    } 
 
    return mondays; 
 
} 
 

 
// Get all Mondays and display result 
 
// SO console doensn't show all results 
 
var mondays = getMondays(); 
 
mondays.forEach(function(mon) { 
 
    console.log(mon.toLocaleString(void 0, { 
 
    weekday: 'short', 
 
    day: 'numeric', 
 
    month: 'short', 
 
    year: 'numeric' 
 
    })); 
 
}); 
 

 
// Count of Mondays, not all shown in SO console 
 
console.log('There are ' + mondays.length + ' Mondays, the first is ' + mondays[0].toString())

相關問題