2017-07-03 2 views
1

ich Probleme immer haben Termine Funktionen herauszufindenHolen Sie sich alle Mondays im Jahr

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); 
} 

ich diese Funktion bin mit allen Mondays in einem Monat zu bekommen, aktuellen Monat.

Wie kann ich diesen Code anpassen, um alle verbleibenden montags im Jahr zu erhalten?

+0

Am Ende der letzten Runde: 'if (d.getMonth() === 0) brechen;' –

+0

Was ist mit dem 'while (d.getMonth() == = Monat) {'Muss ich es nicht aktualisieren? –

+1

Ändern Sie einfach alles, was sich auf den Monat bezieht, um das Jahr zu betrachten. – Barmar

Antwort

2

Gerade Schleife durch das Jahr anstelle des Monats. Der Code ist der gleiche wie bei Ihnen, es funktioniert einwandfrei. nur geändert Monat -> Jahr und getMonth() -> getYear()

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

verwenden. Fügen Sie keine Tage hinzu, indem Sie 24 Stunden hinzufügen. An Orten, an denen Tageslicht eingespart wird, sind nicht alle Tage 24 Stunden lang. Siehe [* +1 zum aktuellen Datum hinzufügen *] (https://stackoverflow.com/questions/9989382/add-1-to-current-date). – RobG

+0

@RobG weitermachen und bearbeiten. – Rick

0

Dies ist nur eine Alternative, verwendet es ein einfacheres Verfahren zur Herstellung des ersten Tag des Monats bekommen.

// 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())

Verwandte Themen