Skip to content

Commit ff4b4b0

Browse files
committed
[Feature] Started working on DateCountDown js
a function to count the time left from x date to now.
1 parent 0605800 commit ff4b4b0

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

Dist/Functional/DateCountDown.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict'
2+
3+
function isDateValid(dateStr) {
4+
return !isNaN(new Date(dateStr));
5+
}
6+
7+
const InitializeDateCountDown = () => {
8+
9+
const targetDate = document.querySelector('[wt-datecount-element="target-date"]');
10+
11+
if(!targetDate) return;
12+
13+
var countDownDate = new Date(targetDate.innerText).getTime();
14+
15+
if(!isDateValid(countDownDate)) return;
16+
17+
// Update the count down every 1 second
18+
var x = setInterval(function() {
19+
20+
// Get today's date and time
21+
var now = new Date().getTime();
22+
23+
// Find the distance between now and the count down date
24+
var distance = countDownDate - now;
25+
26+
const _targetYear = document.querySelector('[wt-datecount-element="target-year"]');
27+
const _targetMonth = document.querySelector('[wt-datecount-element="target-month"]');
28+
const _targetDay = document.querySelector('[wt-datecount-element="target-day"]');
29+
const _targetHour = document.querySelector('[wt-datecount-element="target-hour"]');
30+
const _targetMin = document.querySelector('[wt-datecount-element="target-minute"]');
31+
const _targetSec = document.querySelector('[wt-datecount-element="target-second"]');
32+
33+
34+
// Time calculations for days, hours, minutes and seconds
35+
var year = Math.floor(distance / (1000 * 60 * 60 * 24));
36+
var month = Math.floor(distance / (1000 * 60 * 60 * 24));
37+
var second = Math.floor(distance / (1000 * 60 * 60 * 24));
38+
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
39+
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
40+
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
41+
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
42+
43+
// Output the result in an element with id="demo"
44+
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
45+
+ minutes + "m " + seconds + "s ";
46+
47+
48+
49+
50+
// If the count down is over, write some text
51+
if (distance < 0) {
52+
clearInterval(x);
53+
document.getElementById("demo").innerHTML = "EXPIRED";
54+
}
55+
}, 1000);
56+
}

Dist/Functional/FormatNumbers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
* currency uses regular notation for them.
1010
*/
1111

12+
'use strict'
1213

1314
let FormatNo = (element, locales, number, options) => {
1415
let final = Intl.NumberFormat(locales, options).format(number);
1516
if (!final) return;
1617
element.innerHTML = final;
1718
}
19+
1820
const InitializeFormatNumbers = () => {
1921
let numbersToFormat = document.querySelectorAll('[wt-formatnumber-element="number"]');
2022

0 commit comments

Comments
 (0)