trying to make a countdown timer to particular date

countdown timer till 27th of july so when i press the button it shows 15 days 5 hrs left for 27th july
1 Reply
Baptiste
Baptiste2mo ago
Maybe try something like
const now = new Date();
const target = new Date({{Date}});
const diffMs = target - now;

if (diffMs <= 0) return "0 days 0 hrs left";

const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
const diffHrs = Math.floor((diffMs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));

return `${diffDays} days ${diffHrs} hrs left`;
const now = new Date();
const target = new Date({{Date}});
const diffMs = target - now;

if (diffMs <= 0) return "0 days 0 hrs left";

const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
const diffHrs = Math.floor((diffMs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));

return `${diffDays} days ${diffHrs} hrs left`;
Where {{Date}} is your target date

Did you find this page helpful?