mirror of
https://github.com/NebulaServices/Nebula.git
synced 2025-05-17 13:30:00 -04:00
working clock
This commit is contained in:
parent
71d65f2cda
commit
ddfb2ec900
1 changed files with 43 additions and 16 deletions
|
@ -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;
|
Loading…
Add table
Add a link
Reference in a new issue