D
D
Dima2020-09-04 08:59:55
JavaScript
Dima, 2020-09-04 08:59:55

Countdown to a specific date?

such a date comes from php (smarty template engine) 2020-09-06 07:02:00 how to make a countdown to it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Gennady S, 2020-09-04
Dolgoter @SpiderPigAndCat

Well, it's very convenient to use libraries like moment, luxon, in terms of comparison, days iteration API, etc., but you can also use the standard Date, in the standard JS library, the date-time class is cool : ) As an example:

let source = new Date('2020-09-06 07:02:00');
let current = new Date();
while (source.getTime() > current.getTime())
   current.setDate(current.getDate() + 1); // просто добавляем дни

- you can count in both directions and at any time get any parts of the date.
If you need to output something along the lines of "so many days left":
let source = new Date('2020-09-06 07:02:00');
let current = new Date();
let diff = new Date(source.getTime() - current.getTime());
let empty = new Date(0);
console.log('Months remains:', diff.getMonth() - empty.getMonth());
let days = diff.getDate() - empty.getDate();
let hours = diff.getHours() - empty.getHours();
console.log('Days remains:', hours < 0 ? days - 1: days); // это справедливо и для месяцев, дней
console.log('Hours remains:', hours < 0 ? 24 + hours : hours);

- I came up with this solution on the go, perhaps they will offer it more efficiently.

D
Dima, 2020-09-04
Dolgoter

found what you need https://codepen.io/ParsonsProjects/pen/mVbZgY?edit...
here on line 4 just replace the date with your own and check the sequence of days of years and months
thanks for the hint about moment js

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question