Help me write the code of the action. There is a day, for example, today is 15, the share is up to the 19th day, the 16th number is also 19, but when the 19th day comes, the stock changes to 23, that is, every 4 days the number in the block changes, how to solve? I understand that something like this, but then nothing climbs into my head (

var date = new Date(); var target = 15; var now = date.getDate(); if (now === target) { // do something target += 4; } 
  • Calculate the number of days to zero point, divide by four, add one, multiply by 4. - etki
  • If I understand the question correctly, then you have a number 19 somewhere, which should come from somewhere, or there should be a starting point, for example, the beginning of the month, this moment is not completely understood. Explain where this data comes from from the database, or according to some algorithm? - Sergey Glazirin
  • Sergey Glazirin I myself still do not quite understand how and where to go next, it is better to write an algorithm, since the database will have to be connected) - Artem Efremov
  • etki can be an example? otherwise it’s not entirely clear whether the dates will change or they will be static) - Artyom Efremov

1 answer 1

If I understood the wording correctly, then:

  • There is some text output like "The action lasts until DD.MM.YYYY"
  • It is necessary to withdraw DD.MM.GG such that until the end of the action always remains from 4 to 1 day, but when this date comes - to extend the action for 4 days

As a result, the user will always see the "hot share", to the end of which less than 4 days are left, but there will be no obvious deception - within 4 days the share is up to the same number

Open the Javascript Date () class description in any google

For simplicity, we will use the day number in the month getDate() (not the most obvious name) for the start of the action (that is, from 1 to 4 numbers, from 5 to 9, from 10 to 14, and so on)

There is a current date. We need to know when the rally will end at the current date. Obviously, the next day, (number-1) of which will be completely divided by 4. Minus one because the numbering of days starts from 1, and numbers in mathematics start from 0.

 /* Текущая дата и время */ var now = new Date(); /* Для отладки вы можете использовать не сегодняшнюю дату, а пример ниже */ // now = new Date(2017,4,14,2,31,30); alert('Сегодня '+now.toLocaleString()); /* До тех пор, пока (номер дня - 1) не делится нацело на 4, прибавляем 1 день к дате */ while ((now.getDate()-1) % 4 != 0) { now.setDate(now.getDate()+1); } alert('Акция закончится '+now.toLocaleString()); 

A more "scary" option, but a more reliable one is to count in seconds from the beginning of an epoch:

 /* Текущая дата и время */ var now = new Date(); alert('Сегодня '+now.toLocaleString()); /* До тех пор, пока (номер дня - 1) не делится нацело на 4, прибавляем 1 день к дате */ var one_day = 1*24*60*60*1000; // Один день в миллисекундах var period = 4*24*60*60*1000; // Период обновления акции в миллисекунды // Прежде чем двигать время, надо округлить до целых суток, иначе имеем бесконечный цикл now.setTime(now.getTime() - now.getTime() % one_day); while ((now.getTime() % period) != 0) { now.setTime(now.getTime()+one_day); } alert('Акция закончится '+now.toLocaleString()); 

The logic is the same, only the 4 day block is translated into seconds. Subtleties - time zone

  • Thanks for the detailed answer! If I understand correctly, it is now shown that the action will end on the 17th, after the onset of the 17th will be +4 days, that is, the 21st? - Artem Efremov
  • Well, make the assignment now = new Date(2018,1,18,2,31,30); in the first example and see for yourself - wirtwelt
  • In fact, the first example is bad) There was an error buried in the transition in a month. But this is just to understand the logic of the decision. I advise you to understand the logic of the first example and immediately go into the implementation of the second example, modifying it - wirtwelt
  • Good! I understand, thanks for the help! - Artem Efremov