added date to clock

This commit is contained in:
Niklas 2020-04-22 16:15:45 +02:00
parent 2ab567d3ae
commit 8e185d9f98
2 changed files with 24 additions and 5 deletions

View File

@ -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,7 +14,8 @@ class Clock extends React.Component<{}, State> {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
time: "12:00" time: "",
date: ""
}; };
} }
@ -26,6 +28,13 @@ class Clock extends React.Component<{}, State> {
}); });
} }
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>;
} }

View File

@ -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;
} }
} }