Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.

Commit 3a28993

Browse files
authored
Merge pull request #3767 from cezaraugusto/clock
Add clock component
2 parents 78583ee + 8a09541 commit 3a28993

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

js/about/clock.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
const React = require('react')
6+
7+
class Clock extends React.Component {
8+
constructor () {
9+
super()
10+
this.state = {}
11+
}
12+
get currentTime () {
13+
const currentdate = new Date()
14+
let hours = currentdate.getHours()
15+
let minutes = currentdate.getMinutes()
16+
let timeOfDay = (hours < 12) ? 'am' : 'pm'
17+
// Set hours to be between 0 - 12
18+
// and minutes less than 10 to have a leading zero
19+
hours = (hours > 12) ? hours - 12 : hours
20+
hours = (hours === 0) ? 12 : hours
21+
minutes = (minutes < 10 ? '0' : '') + minutes
22+
23+
return {
24+
time: `${hours}:${minutes}`,
25+
dayTime: `${timeOfDay}`
26+
}
27+
}
28+
updateClock () {
29+
this.setState({
30+
currentTime: this.currentTime.time,
31+
dayTime: this.currentTime.dayTime
32+
})
33+
}
34+
componentDidMount () {
35+
window.setInterval(this.updateClock.bind(this), 1000)
36+
}
37+
render () {
38+
return <div className='clock'>
39+
<span className='time'>{this.state.currentTime}</span>
40+
<span className='dayTime'>{this.state.dayTime}</span>
41+
</div>
42+
}
43+
}
44+
module.exports = Clock

0 commit comments

Comments
 (0)