-
Notifications
You must be signed in to change notification settings - Fork 3
/
DigitalClock.js
60 lines (50 loc) · 1.35 KB
/
DigitalClock.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
import React, { Component } from 'react'
import {
View,
Text,
StyleSheet
} from 'react-native'
class DigitalClock extends Component {
constructor(props) {
super(props)
this.state = {
liveTime: '',
}
let timeFormater = this.timeFormater
this.getTime = setInterval(() => {
let currentDate = new Date()
let timeFormat = `${timeFormater(currentDate.getHours())}:${timeFormater(currentDate.getMinutes())}:${timeFormater(currentDate.getSeconds())}`
this.setState({
liveTime: timeFormat
})
}, 1000)
}
timeFormater(time) {
if (time < 10) {
time = '0' + time
}
return time
}
componentWillUnmount() {
clearInterval(this.getTime)
}
render() {
return (
<View style={this.props.clockWrapperStyles}>
<Text style={[styles.clockText, this.props.clockStyles]}>{this.state.liveTime}</Text>
</View>
)
}
}
const styles = StyleSheet.create({
clockText: {
color: '#000',
fontSize: 18,
fontWeight: '500'
}
})
DigitalClock.propTypes = Object.assign({}, Component.propTypes, {
clockWrapperStyles: View.propTypes.style,
clockStyles: Text.propTypes.style,
})
export default DigitalClock