working clock

This commit is contained in:
rift 2023-12-10 19:36:45 -06:00
parent 71d65f2cda
commit ddfb2ec900

View file

@ -1,27 +1,54 @@
import { useState, useEffect } from 'preact/hooks';
import { useState, useEffect } from "preact/hooks";
const Clock = () => {
const CurrentTime = () => {
const [currentTime, setCurrentTime] = useState('');
const [currentMinutes, setCurrentMinutes] = useState('');
const [opacity, setOpacity] = useState(0);
useEffect(() => { // Hell
useEffect(() => { // LITERAL HELL
const updateCurrentTime = () => {
const now = new Date();
const dayOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][now.getDay()];
let hours = now.getHours();
hours = hours % 12 || 12;
hours = hours % 12 || 12; // 12 hour format
const minutes = now.getMinutes();
const formattedTime = `${dayOfWeek}, ${hours < 10 ? '0' : ''}${hours}`;
setCurrentTime(formattedTime);
};
const updateCurrentMinutes = () => {
const now = new Date();
const minutes = now.getMinutes();
const formattedMinutes = `${minutes < 10 ? '0' : ''}${minutes}`;
setCurrentMinutes(formattedMinutes);
};
updateCurrentTime();
const intervalId = setInterval(updateCurrentTime, 1000);
updateCurrentMinutes();
const timeIntervalId = setInterval(updateCurrentTime, 60000);
const minutesIntervalId = setInterval(updateCurrentMinutes, 1000);
return () => {
clearInterval(timeIntervalId);
clearInterval(minutesIntervalId);
};
}, []);
useEffect(() => {
const intervalId = setInterval(() => {
setOpacity((prevOpacity) => (prevOpacity === 0 ? 1 : 0));
}, 1000);
return () => clearInterval(intervalId);
}, []);
return (
<h1>{currentTime}</h1>
<div class="flex flex-row">
<div>{currentTime}</div>
<div style={{ opacity }}>:</div>
<div>{currentMinutes} PM</div>
</div>
);
};
};
export default Clock;
export default CurrentTime;