1

I'm trying to switch text between Week 1 and Week 2 every week depending on an array of dates.

Currently it works but only for specified dates, I'm trying to make it keep the text for the whole week until it reaches the next date.

So if the date is Monday 16th April, I want it to display Week 1 for the whole week until it reaches Monday 23rd April.

If not that then would it be possible to change text every Sunday and keep the same text til the following Sunday?

Code

var todaysdate = new Date();
var dd = todaysdate.getDate();
var mm = todaysdate.getMonth()+1; //January is 0!

var yyyy = todaysdate.getFullYear();
if(dd<10){
    dd='0'+dd;
} 
if(mm<10){
    mm='0'+mm;
} 
var todaysdate = dd+'/'+mm+'/'+yyyy;

var weekadates = 
["17/04/2018", "30/04/2018",//April
"14/05/2018",               //May
"04/06/2018", "18/06/2018",//June
"02/07/2018", "16/07/2018",//July
];

var weekbdates = 
["23/04/2018",             //April
"07/05/2018", "21/05/2018",//May
"11/06/2018", "25/06/2018",//June
"09/07/2018"               //July
];





var a = weekadates.indexOf(todaysdate);
var b = weekbdates.indexOf(todaysdate);


if (a > -1) 
{
    document.getElementById("changeText").innerHTML = "Week 1";
} 
else if (b > -1) 
{
    document.getElementById("changeText").innerHTML = "Week 2"; 
} else
{
    document.getElementById("changeText").innerHTML = "Holidays!";
}

https://jsfiddle.net/naLuny2x/2/

Thanks for your help, much appreciated.

3 Answers3

0

When trying to find the date in the array, you shouldn't be using today's date, but rather the date of Monday in the current week:

const monday = new Date();
monday.setDate(monday.getDate() - ((monday.getDay() + 6) % 7));

And then use that in the rest of your logic.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
0

Use the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.

{
    function oddEvenWeek ( baseDate = "04/17/2018", current = Date.now() ) {
        let base = new Date( baseDate ).getTime();
        let now = new Date( current ).getTime();
        let fromBase = ( now - base ) / ( 1000 * 60 * 60 * 24 * 7 ); //Weeks from base date
        let weeks = fromBase % 2;
        if ( weeks % 1 >= 6 / 7 ) {
            console.log( "Holiday" );
        } else {
            if ( ( fromBase % 2 ) < 1 ) {
                console.log( "Week 1" );
            } else {
                console.log( "Week 2" );
            }
        }
    }

    oddEvenWeek("04/17/2018","04/17/2018");
    oddEvenWeek("04/17/2018","04/23/2018");
    oddEvenWeek("04/17/2018","04/24/2018");
    oddEvenWeek("04/17/2018","04/30/2018");
}
Isitea
  • 741
  • 5
  • 13
  • This has solved my issue, thank you very much, much appreciated. –  Apr 18 '18 at 12:12
  • @jdoe2168 What means “add next month”? – Isitea Apr 23 '18 at 08:48
  • Isitea, Can I ask what is passed into the function? I'm guessing the base date and the week 1 dates? For some reason, when I add a Week 1 date for next month, (05/07/18) it changes the output to Week 1 when this week (WC 04/23/18) is meant to be Week 2. Is the base date supposed to change at all?, Apologies and thanks for your help. Sorry, Isitea, I pressed Enter by accident lol xD –  Apr 23 '18 at 09:10
  • @jdoe2168 function parameter `baseDate` means start-day for counting. And second parameter `current` means target date to know week type. If you leave `current` as undefined, `current` will be assigned as Date.now() which indicate 'current time since 1970.01.01 00:00 as ms. – Isitea Apr 23 '18 at 09:16
0

I found a code snippet to find out the calendar week here:

/**
 * Returns the week number for this date.  dowOffset is the day of week the week
 * "starts" on for your locale - it can be from 0 to 6. If dowOffset is 1 (Monday),
 * the week returned is the ISO 8601 week number.
 * @param int dowOffset
 * @return int
 */
Date.prototype.getWeek = function (dowOffset) {
/*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.meanfreepath.com */

    dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero
    var newYear = new Date(this.getFullYear(),0,1);
    var day = newYear.getDay() - dowOffset; //the day of week the year begins on
    day = (day >= 0 ? day : day + 7);
    var daynum = Math.floor((this.getTime() - newYear.getTime() - 
    (this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
    var weeknum;
    //if the year starts before the middle of a week
    if(day < 4) {
        weeknum = Math.floor((daynum+day-1)/7) + 1;
        if(weeknum > 52) {
            nYear = new Date(this.getFullYear() + 1,0,1);
            nday = nYear.getDay() - dowOffset;
            nday = nday >= 0 ? nday : nday + 7;
            /*if the next year starts before the middle of
              the week, it is week #1 of that year*/
            weeknum = nday < 4 ? 1 : 53;
        }
    }
    else {
        weeknum = Math.floor((daynum+day-1)/7);
    }
    return weeknum;
};

Then you can calculate with the modulo (%) operator if it's an even or uneven week:

var todaysdate = new Date();
var dd = todaysdate.getDate();
if(dd === 1) {
    document.getElementById("changeText").innerHTML = "Week " + (todaysdate.getWeek() % 2 + 1);
} else {
    document.getElementById("changeText").innerHTML = "Holidays!"
}
Kokogino
  • 984
  • 1
  • 5
  • 17