-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurrentDate.jsx
62 lines (51 loc) · 1.35 KB
/
CurrentDate.jsx
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
import React from 'react';
class CurrentDate extends React.Component {
constructor(props) {
super(props);
const time = new Date();
this.state = {
currentDate: time.getDate(),
formattedCurrentDate: this._formatCurrentDate(time),
intervalId: null,
};
}
_formatCurrentDate(time) {
let currentDate = time.toLocaleString(
'en-US',
{
// Since we're always concerned with Boston we know the timezone
timeZone: 'America/New_York',
day: 'numeric',
month: 'numeric',
year: 'numeric',
}
);
// Replacing slashes with hyphens to match image of board at North Station
return currentDate.replace(/\//g, '-');
}
updateDate() {
this.setState({
formattedCurrentDate: this._formatCurrentDate(new Date()),
});
}
componentDidMount() {
// Keep track of the setInterval ID so we can clear it later
const intervalId = setInterval(() => {
const time = new Date();
if (time.getDate() !== this.state.currentDate) {
this.updateDate();
}
}, this.props.checkInterval || 1000);
this.setState({
intervalId: intervalId,
});
}
render() {
return (
<div style={{ position: 'absolute', marginLeft: '1em', }}>
{this.state.formattedCurrentDate}
</div>
);
}
}
export default CurrentDate;