added date to clock
This commit is contained in:
parent
2ab567d3ae
commit
8e185d9f98
@ -4,6 +4,7 @@ import "../style/clock.scss";
|
|||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
time: string;
|
time: string;
|
||||||
|
date: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
class Clock extends React.Component<{}, State> {
|
class Clock extends React.Component<{}, State> {
|
||||||
@ -13,19 +14,27 @@ class Clock extends React.Component<{}, State> {
|
|||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
this.state = {
|
this.state = {
|
||||||
time: "12:00"
|
time: "",
|
||||||
|
date: ""
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
updateTime() {
|
updateTime() {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
|
||||||
// Is there a native function for formatting time ??
|
// Is there a native function for formatting time ??
|
||||||
this.setState({
|
this.setState({
|
||||||
time: `${this.zeroPad(now.getHours(), 2)}:${this.zeroPad(now.getMinutes(), 2)}`
|
time: `${this.zeroPad(now.getHours(), 2)}:${this.zeroPad(now.getMinutes(), 2)}`
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateDate() {
|
||||||
|
const now = new Date();
|
||||||
|
this.setState({
|
||||||
|
date: `${now.toDateString()}`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
zeroPad(number: number, pad: number): string {
|
zeroPad(number: number, pad: number): string {
|
||||||
const length = number.toString().length;
|
const length = number.toString().length;
|
||||||
if (length < pad) {
|
if (length < pad) {
|
||||||
@ -38,11 +47,15 @@ class Clock extends React.Component<{}, State> {
|
|||||||
startTimer() {
|
startTimer() {
|
||||||
this.interval = setInterval(()=>{
|
this.interval = setInterval(()=>{
|
||||||
this.updateTime();
|
this.updateTime();
|
||||||
|
|
||||||
|
// This will change only if the tab stays open after midnight. But still, lets cover that
|
||||||
|
this.updateDate();
|
||||||
},1000);
|
},1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.updateTime();
|
this.updateTime();
|
||||||
|
this.updateDate();
|
||||||
this.startTimer();
|
this.startTimer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,6 +68,9 @@ class Clock extends React.Component<{}, State> {
|
|||||||
<div className="time">
|
<div className="time">
|
||||||
{this.state.time}
|
{this.state.time}
|
||||||
</div>
|
</div>
|
||||||
|
<div className="date">
|
||||||
|
{this.state.date}
|
||||||
|
</div>
|
||||||
</div>;
|
</div>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
.clock-component{
|
.clock-component{
|
||||||
|
text-align: center;
|
||||||
|
|
||||||
.time{
|
.time{
|
||||||
font-size: 10em;
|
font-size: 10em;
|
||||||
text-align: center;
|
}
|
||||||
|
|
||||||
|
.date {
|
||||||
|
font-size: 1em;
|
||||||
|
margin-top: -1em;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user