forked from coderplex-org/coderplex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount-down.js
140 lines (121 loc) · 3.18 KB
/
count-down.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React, { Component } from 'react';
import PropTypes from 'prop-types';
class Countdown extends Component {
constructor(props) {
super(props);
this.state = {
days: 0,
hours: 0,
min: 0,
sec: 0,
};
}
componentDidMount() {
// Update every second
this.interval = setInterval(() => {
const date = this.calculateCountdown(this.props.date);
if (date) this.setState(date);
else this.stop();
}, 1000);
}
componentWillUnmount() {
this.stop();
}
calculateCountdown(endDate) {
let diff = (Date.parse(new Date(endDate)) - Date.parse(new Date())) / 1000;
// Clear countdown when date is reached
if (diff <= 0) return false;
const timeLeft = {
years: 0,
days: 0,
hours: 0,
min: 0,
sec: 0,
millisec: 0,
};
// Calculate time difference between now and expected date
if (diff >= 365.25 * 86400) {
// 365.25 * 24 * 60 * 60
timeLeft.years = Math.floor(diff / (365.25 * 86400));
diff -= timeLeft.years * 365.25 * 86400;
}
if (diff >= 86400) {
// 24 * 60 * 60
timeLeft.days = Math.floor(diff / 86400);
diff -= timeLeft.days * 86400;
}
if (diff >= 3600) {
// 60 * 60
timeLeft.hours = Math.floor(diff / 3600);
diff -= timeLeft.hours * 3600;
}
if (diff >= 60) {
timeLeft.min = Math.floor(diff / 60);
diff -= timeLeft.min * 60;
}
timeLeft.sec = diff;
return timeLeft;
}
stop() {
clearInterval(this.interval);
}
addLeadingZeros(value) {
value = String(value);
while (value.length < 2) {
value = '0' + value;
}
return value;
}
render() {
const countDown = this.state;
return (
<div className="countdown">
<span className="countdown-col">
<span className="countdown-col-element">
<strong>{this.addLeadingZeros(countDown.days)}</strong>
<span>{countDown.days === 1 ? 'Day' : 'Days'}</span>
</span>
</span>
<span className="countdown-col">
<span className="countdown-col-element">
<strong>{this.addLeadingZeros(countDown.hours)}</strong>
<span>Hours</span>
</span>
</span>
<span className="countdown-col">
<span className="countdown-col-element">
<strong>{this.addLeadingZeros(countDown.min)}</strong>
<span>Min</span>
</span>
</span>
<span className="countdown-col">
<span className="countdown-col-element">
<strong>{this.addLeadingZeros(countDown.sec)}</strong>
<span>Sec</span>
</span>
</span>
<style jsx>{`
.countdown {
display: flex;
justify-content: space-around;
padding: 15px 0;
}
.countdown-col {
}
.countdown-col-element {
display: flex;
flex-direction: column;
align-items: center;
}
`}</style>
</div>
);
}
}
Countdown.propTypes = {
date: PropTypes.string.isRequired,
};
Countdown.defaultProps = {
date: new Date(),
};
export default Countdown;