2016-07-11 9 views
0

Ziel: Zeigt automatisch das Datum eines bevorstehenden zweiwöchentlichen Ereignisses an. In diesem Fall jeden Samstag. Idealerweise durch Modifikation des bestehenden Codes.Datum Objektalgorithmus zur Rückgabe des nächsten zweiwöchentlichen Wochentages

z.B. Wenn eine Veranstaltung am 9. Juli stattfindet und heute am 11. Juli ist, ist die nächste Veranstaltung der 23. Juli. Wenn heute der 24. Juli ist, ist die nächste Veranstaltung der 6. August und so weiter ...

Problem: How to Berechnen Sie das Datum? (Ich nehme an, dass ein Referenzstartdatum benötigt wird, beispielsweise 9. Juli 2016)

Hintergrund/Vorhandener Code: Berechnen Sie das Datum eines nächsten gegebenen Wochentages. (Wenn heute Montag der 14. ist, was ist das Datum des kommenden Samstags?). HINWEIS: In diesem Fall ist das Ereignis jeden Freitag & Samstag. Wir zeigen die Daten für kommenden Freitag und Samstag an. ABER ich wollte sicherstellen, dass die angezeigten Daten immer am selben Wochenende fallen. (Wenn heute Samstag ist, sollte FR-Datum gestern sein, und SAT-Datum sollte heute sein).

(function() { 

    // Today's date 
    var startingDate = new Date(); 

    // Find date of nearest day of week 
    function dateOfNearestDay(startingDate, nearestDay) { 

    // Date object to work with inside the function 
    var nearestTime = new Date(startingDate.getTime()); 

    // If today is Sat, use last Fri's date, else calculate date of nearest day 
    if (startingDate.getDay() === 6 && nearestDay === 5) { 

     nearestTime.setDate((startingDate.getDate() + (7 + nearestDay - startingDate.getDay()) % 7) - 7); 

    } 
    else { 

     nearestTime.setDate(startingDate.getDate() + (7 + nearestDay - startingDate.getDay()) % 7); 

    } 

    return nearestTime; 
    } 

    // Format date as needed for display 
    function getMonthAndDay(date) { 

    // Array to store month names for translation 
    monthNames = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.']; 

    return monthNames[date.getMonth()]+ " " + date.getDate(); 
    } 

    // Get date object of nearest Friday and Saturday 
    var friday = dateOfNearestDay(startingDate, 5); 
    var saturday = dateOfNearestDay(startingDate, 6); 

    var fridayDate = document.getElementById('friday'); 
    var saturdayDate = document.getElementById('saturday'); 

    // Write month & day of nearest day 
    fridayDate.textContent = getMonthAndDay(friday);  
    saturdayDate.textContent = getMonthAndDay(saturday); 

}()); 
+0

was wird es am 23. Juli zeigen? der nächste bis nächste Samstag Datum oder Datum dieses Tages? –

Antwort

0

Die Funktion nextEvent für ein bestimmtes Datum today und bekannt Datum der event berechnet den nächsten zwei Wochen wiederkehrende Ereignis nach today oder today:

// Today's date 
 
var startingDate = new Date(); 
 
startingDate.setDate(startingDate.getDate()); 
 

 
// Find date of nearest day of week 
 
function dateOfNearestDay(startingDate, nearestDay) { 
 

 
    // Date object to work with inside the function 
 
    var nearestTime = new Date(startingDate.getTime()); 
 

 
    // If today is Sat, use last Fri's date, else calculate date of nearest day 
 
    if (startingDate.getDay() === 6 && nearestDay === 5) { 
 

 
     nearestTime.setDate((startingDate.getDate() + (7 + nearestDay - startingDate.getDay()) % 7) - 7 - 7); 
 

 
    } else { 
 

 
     nearestTime.setDate(startingDate.getDate() + (7 + nearestDay - startingDate.getDay()) % 7 - 7); 
 

 
    } 
 

 
    return nearestTime; 
 
} 
 

 
// For a given today and last event finds closest next event from today. 
 
// Assumption is that event is recurring every 14 days 
 
function nextEvent(today, event) { 
 
    var eventRecurrence = 14; // recurrence of the event in days 
 
    var nextEventTime = new Date(today.getTime()); 
 
    nextEventTime.setDate(today.getDate() + ((today.getDate() > event.getDate()) ? eventRecurrence : 0) - (today.getDate() - event.getDate()) % eventRecurrence); 
 
    return nextEventTime; 
 
} 
 

 
// Format date as needed for display 
 
function getMonthAndDay(date) { 
 

 
    // Array to store month names for translation 
 
    monthNames = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.']; 
 

 
    return monthNames[date.getMonth()] + " " + date.getDate(); 
 
} 
 

 
// Get date object of nearest Friday and Saturday 
 
var friday = dateOfNearestDay(startingDate, 5); 
 
var saturday = dateOfNearestDay(startingDate, 6); 
 

 
var nextFridayEvent = nextEvent(startingDate, friday); 
 
var nextSaturdayEvent = nextEvent(startingDate, saturday); 
 

 
// Write months & days of the variables 
 
console.log('Today is ' + getMonthAndDay(startingDate)); 
 
console.log('Friday Event ' + getMonthAndDay(friday)); 
 
console.log('Saturday Event ' + getMonthAndDay(saturday)); 
 
console.log('Next Friday Event ' + getMonthAndDay(nextFridayEvent)); 
 
console.log('Next Saturday Event ' + getMonthAndDay(nextSaturdayEvent))

ich geändert haben Ihre Funktion dateOfNearestDay mit 2x -7 um zu demonstr aß Ereignisse vor today.