The page should display the date of the next Monday. For example. If we go to the page today, then "February 13" is displayed. Also, if we go to the page before 7 pm on February 13, the date does not change. But if we go on February 13 after 7 o'clock in the evening, it is already displayed on February 20, etc. How can this be implemented? I can not write the code, because can't even imagine where to start.

    2 answers 2

    var d = new Date(); var monthNames = new Array("Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь"); //допустим сегодня 13 текущего месяца d.setDate(13); var currentHour = d.getHours(); // 18 для проверки if(currentHour < 19) { // до 19:00, показываем текущий понедельник d.setDate(d.getDate() + (1 + 7 - d.getDay()) % 7); } else { //после 19:00, показываем следующий понедельник d.setDate(d.getDate() + (7-d.getDay())%7+1); } alert(d.getDate() + ' - ' + monthNames[d.getMonth()]); 

    • Can you give a short comment on the script? I would like to understand how it works :-) - Anton Petrenko
    • If I change the date on the computer on February 13th. and I now have time 20:26, but all the same date displays February 13. And I can not understand how to edit time here (7 pm). - Anton Petrenko
    • one
      Added a time check, it seems to work, and the formulas are the first to fall into the online. - Ruslan_K
    • Damn, how to figure it out :-) This is d.setDate (13); what is it used for? - Anton Petrenko
    • And if today is not 13? - Anton Petrenko

    You can use the library moment. Next Monday will be:

     moment().day(8).format('DD.MM.YYYY') 

    the day method selects the day relative to the set number. For example,

     moment().day(1).format('DD') 

    will show the date for Monday which this week.