This repository was archived by the owner on Dec 11, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments