How to calculate the number of days before a certain date

The case is useful if you need to inform the user how many days are left until the promotion ends or in other similar cases when it is necessary to make a day counter before a given date in the bot.

In the example, we will count the days from today to a date in the future. This date, for example, may be the last day of discounts on purchases through the bot.

If necessary, you can replace today's date with any other date or ask the user to send the date and record it using User Input → with the Date data type. Based on this case, you can create other logic options, for example, you can count days from a date in the past to today’s date, or calculate the number of days between different dates, etc.

The essence boils down to three actions - first we convert dates into milliseconds, do the necessary mathematical calculations, then we convert dates from milliseconds to days using division.

The number of days can be delivered to the user in a broadcast or used in any other way.

1. Get today's date in milliseconds using the {{now}} helper

2. Write down the day the promotion ends using Assign a Variable and immediately convert it into milliseconds

To do this, use the helper

{{formatDate futureDate 'x'}}

where futureDate is the date until which days are counted.

3. After the dates are converted to milliseconds, you can subtract one date from another one using the subtraction helper:

{{subtract future Date today}}

where futureDate is a future date, and today is a today’s date.

subtract is used in helpers for mathematical actions, you can learn more about this in the article Mathematics →

4. We have just received the result we need — the number of days from today to the date the promotion ends. Now the result is in milliseconds, we will gradually convert it into the number of days.

Here we will also use Write Variable, we will write in turn the helpers in variables values, the names of variables can be different if it is necessary to record intermediate results.

If all calculations are recorded in one variable, the final result will remain at the end. This can be done not to produce unnecessary variables if intermediate results are not needed.

  • convert the result into seconds, for this we divide the total by 1000: {{divide (subtract futureDate today) 1000}}
  • then convert the result into minutes, for this we divide what happened by 60: {{divide (divide (subtract futureDate today) 1000) 60}}
  • to get hours, divide again the last result by 60: {{divide (divide (divide (subtract futureDate today) 1000) 60) 60}}
  • to find out the number of days, divide everything you have by 24: {{divide (divide (divide (subtract futureDate today) 1000) 60) 60) 24}}
  • round up the result converted into days using the floor helper {{floor (divide (divide (divide (divide (subtract futureDate today) 1000) 60) 60) 24)}}

This chain can be simplified by counting the numeric part on the calculator once — 86400000.

After we subtract one number from another, it remains only to divide the result in milliseconds by this number, then round it off.

{{divide (subtract to from) 86400000}}
{{floor (divide (subtract to from) 86400000)}}

All calculations are also done by Assign Variable:

The {{#vars}} helper will help to cope with this task even faster, for this it is enough to write both dates in milliseconds, as in the previous example, then insert the #vars block with variables into the text, where the numbers from the futureDate and today variables are written.

{{#vars}}
{{var "ms" (subtract futureDate today) ~}}
{{var "sec" (divide @vars.ms 1000) ~}}
{{var "min" (divide @vars.sec 60) ~}}
{{var "h" (divide @vars.min 60) ~}}
{{var "d" (divide @vars.h 24) ~}}
{{var "diff" (floor @vars.d) ~}}
{{@vars.diff}} days left until the end of the promotion
{{/vars}}

To the beginning ↑