Answer the question
In order to leave comments, you need to log in
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
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); // просто добавляем дни
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);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question