mue/src/components/Clock.jsx

43 lines
972 B
React
Raw Normal View History

2019-10-21 19:04:30 +00:00
//* Imports
2019-10-20 12:39:01 +00:00
import React from 'react';
export default class Clock extends React.Component {
constructor(...args) {
super(...args);
this.state = {
2019-11-29 12:12:34 +00:00
date: '',
ampm: '',
2019-10-20 12:39:01 +00:00
};
}
startTime() {
2019-11-29 12:12:34 +00:00
const t = new Date(); // Get the current date
2019-12-26 12:10:39 +00:00
const a = t.getHours();
2019-11-29 12:16:43 +00:00
let h = t.getHours(); // Get hours
2019-10-20 12:39:01 +00:00
// const s = today.getSeconds();
2019-11-29 13:26:36 +00:00
if (h > 12) h = h - 12; // 12 hour support
2019-10-20 12:39:01 +00:00
2019-11-29 12:12:34 +00:00
this.setState({
2019-12-26 12:10:39 +00:00
date: `${('0' + h).slice(-2)}:${('0' + t.getMinutes()).slice(-2)}`, ampm: a >= 12 ? 'PM' : 'AM'
2019-11-29 12:12:34 +00:00
}); // Set time
2019-10-20 12:39:01 +00:00
2019-12-13 12:58:51 +00:00
this.timeout = setTimeout(() => this.startTime(), 750); // Update the clock every 750 milliseconds
2019-10-20 12:39:01 +00:00
}
componentDidMount() {
const enabled = localStorage.getItem('time');
if (enabled === 'false') return;
2019-10-20 12:39:01 +00:00
this.startTime();
}
render() {
2019-10-21 19:04:30 +00:00
return <h1 className='clock'>
2019-10-20 12:39:01 +00:00
{this.state.date}
2019-10-21 19:04:30 +00:00
<span className='ampm'>
2019-10-20 12:39:01 +00:00
{this.state.ampm}
</span>
</h1>;
}
}