-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

2
  • You can start by getting the number of a week and use that to index an array of "week names" Commented Oct 28, 2022 at 19:27
  • What do you mean by "no code trial"? Commented Oct 28, 2022 at 20:34

1 Answer 1

-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]}`
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the response and assistance, I appreciate 💯
There was a space to enter attempted code, so I replied No code trial
I have tried using the second approach when I copy and paste I get an error message const weekNames = ["Morocco", "South Africa", ...] const currentWeek = getWeekNumber(new Date()) const formattedText = Week${currentWeek}: ${weekNames[currentWeek]}
What is the error message?
I will forward a screenshot through a link on the main question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.