-1

error message screenshotA screenshot of website interfaceMy day will be made more beautiful if I get some assistance. Using JavaScript or any other language is there a way I can develop a weekly counter that will display certain text per week, for instance, Week1 - Algeria, Week 2 - South Africa down to week 52 on a website I am developing

Still making research regarding this, no-coding trial yet

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • You can start by [getting the number of a week](https://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php) and use that to index an array of "week names" – LeoDog896 Oct 28 '22 at 19:27
  • What do you mean by "no code trial"? – LeoDog896 Oct 28 '22 at 20:34

1 Answers1

-1

You can start by getting the number of a week and use that to index an array of "week names", or also use a time library to achieve the same goal. The following code uses the former:

/* For a given date, get the ISO week number */
function getWeekNumber(d) {
    // Copy date so don't modify original
    d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
    // Set to nearest Thursday: current date + 4 - current day number
    // Make Sunday's day number 7
    d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7));
    // Get first day of year
    const yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
    // Calculate full weeks to nearest Thursday
    const weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7);
    // Return array of year and week number
    return [d.getUTCFullYear(), weekNo];
}

const weekNames = ["Morocco", "South Africa", ...]
const currentWeek = getWeekNumber(new Date())

const formattedText = `Week${currentWeek}: ${weekNames[currentWeek]}`
LeoDog896
  • 3,472
  • 1
  • 15
  • 40