1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to count “+1” when a specific date occurs

Discussion in 'jQuery' started by AlessandroEnglish, Nov 5, 2019.

  1. #1
    Hello everyone!

    I have this code through which I show a future working day, thus excluding weekends. I would also like to exclude personalized holiday dates, in which case the same thing happens (count ++, I guess).
    SEMrush
    For example, I would like to exclude these dates: ["2019-11-6", "2019-11-13"] How can I integrate this into the code?

    I need this to show a future delivery date on a business day, excluding weekends and some custom dates.

    I've read and tested loads of guides, but I haven't found anything that works.

    jQuery(function($) {
      var monthNames = ["gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"];
      var dayNames = ["domenica", "lunedì", "martedì", "mercoledì", "giovedì", "venerdì", "sabato"]
    
      var endDate = "",
        noOfDaysToAdd = 1,
        count = 0;
      var someDate = new Date();
      var numberOfDaysToAdd = noOfDaysToAdd;
      someDate.setDate(someDate.getDate());
    
      while (count < noOfDaysToAdd) {
        endDate = new Date(someDate.setDate(someDate.getDate() + 1));
        if (endDate.getDay() != 0 && endDate.getDay() != 6) {
          count++;
        }
      }
    
      $('#Date').html(dayNames[endDate.getDay()] + ' ' + endDate.getDate() + ' ' + monthNames[endDate.getMonth()] + ' ' + endDate.getFullYear());
    });
    Code (JavaScript):


    The code works well for the exclusion of weekends, but I would like help to add customized holiday dates as well.

    Thank you very much!
     
    Last edited: Nov 5, 2019
    AlessandroEnglish, Nov 5, 2019 IP
    SEMrush
  2. AlessandroEnglish

    AlessandroEnglish Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    After several researches I found the solution and it seems to work perfectly.
    Here is the working code for those interested:

    jQuery(function($) {
      var monthNames = ["gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"];
      var dayNames = ["domenica", "lunedì", "martedì", "mercoledì", "giovedì", "venerdì", "sabato"];
      var holidays = ["2019, 11, 8", "2019, 11, 11", "2019, 11, 12", "2019, 11, 13", "2019, 11, 14", "2019, 11, 15", "2019, 11, 18"];
      var endDate = "",
        noOfDaysToAdd = 3,
        count = 0;
      var someDate = new Date(new Date().toDateString());
      var numberOfDaysToAdd = noOfDaysToAdd;
      someDate.setDate(someDate.getDate());
      while (count < noOfDaysToAdd) {
        endDate = new Date(someDate.setDate(someDate.getDate() + 1));
        var isHoliday = holidays.find(holiday => endDate.getTime() == new Date(holiday).getTime());
        if (isHoliday) {
          console.log('holiday, skipping');
        } else if (endDate.getDay() != 0 && endDate.getDay() != 6) {
          count++;
        }
      }
    $('#Date').html(dayNames[endDate.getDay()] + ' ' + endDate.getDate() + ' ' + monthNames[endDate.getMonth()]);  
    });
    Code (markup):
     
    AlessandroEnglish, Nov 5, 2019 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,340
    Likes Received:
    1,830
    Best Answers:
    244
    Trophy Points:
    515
    #3
    That got WAY too overcomplicated for it's own good. In particular it's manipulating multiple dates when it only needs ONE to start with and work on.

    
    (function() {
    	var
    		monthNames = [
    			'gennaio', 'febbraio', 'marzo', 'aprile',
    			'maggio', 'giugno', 'luglio', 'agosto',
    			'settembre', 'ottobre', 'novembre', 'dicembre'
    		],
    		dayNames = [
    			'domenica', 'lunedì', 'martedì', 'mercoledì',
    			'giovedì', 'venerdì', 'sabato'
    		],
    		holidays = [ // D M YYYY
    			'8 11 2019', '11 11 2019', '12 11 2019', '13 11 2019',
    			'14 11 2019', '15 11 2019', '18 11 2019'
    		],
    		daysToAdd = 20,
    		dayWalker = new Date();
    	do {
    		var
    			day = dayWalker.getDay(),
    			date = dayWalker.getDate();
    		if (holidays.indexOf(
    			date + ' ' + (dayWalker.getMonth() + 1) + ' ' + dayWalker.getFullYear()
    		) >= 0) console.log('holiday, skipping');
    		else if (day > 0 && day < 6) daysToAdd--;
    		dayWalker.setDate(date + 1);
    	} while (daysToAdd);
    	document.getElementById('Date').textContent =
    		dayNames[dayWalker.getDay()] + ' ' +
    		dayWalker.getDate() + ' ' +
    		monthNames[dayWalker.getMonth()] + ' ' +
    		dayWalker.getFullYear();
    })();
    
    Code (markup):
    If you store your holidays as a easy format, just make that format per day and look for it. Yes, IE8/earlier doesn't have Array.indexOf, polyfill that if you need support for browsers we really should stop supporting.

    See how I store getDate and getDay in variables? I do this as they're each used more than once. Functions are slower than variables, so it's faster to store a value and re-use it than to constantly call the function.

    I also kicked the jQuery mental enfeeblement to the curb. Only thing you can learn from JQ is how NOT to write JavaScript. ESPECIALLY using the slow $().HTML() method that basically calls innerHTML, getting the parser involved and running security risk, when you could just get the ACTUAL element and safely use textContent in a fraction the execution time for little to no extra code.
     
    deathshadow, Nov 5, 2019 IP