forked from b00tc4mp/isdi-parttime-202309
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create Time component, move css styles to tailwind b00tc4mp#403
- Loading branch information
Showing
49 changed files
with
7,698 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React, { useState, useEffect } from "react"; | ||
|
||
export default function Time() { | ||
const [currentDateTime, setCurrentDateTime] = useState(new Date()); | ||
// Efecto para actualizar la hora cada segundo | ||
useEffect(() => { | ||
const intervalID = setInterval(() => { | ||
setCurrentDateTime(new Date()); | ||
}, 1000); // Actualizar cada segundo | ||
|
||
// Limpieza del intervalo para evitar fugas de memoria | ||
return () => clearInterval(intervalID); | ||
}, []); // El efecto se ejecuta solo una vez al montar el componente | ||
|
||
// Función para formatear la hora, minutos y segundos con ceros a la izquierda si es necesario | ||
const formatTimeUnit = (unit) => { | ||
return unit < 10 ? `0${unit}` : unit; | ||
}; | ||
|
||
// Función para formatear la fecha en formato DD/MM/YYYY | ||
const formatDate = (date) => { | ||
const day = formatTimeUnit(date.getDate()); | ||
const month = formatTimeUnit(date.getMonth() + 1); // Los meses comienzan en 0 | ||
const year = date.getFullYear(); | ||
return `${day}/${month}/${year}`; | ||
}; | ||
|
||
return ( | ||
<div> | ||
<p className="time">{`${formatDate(currentDateTime)} ${formatTimeUnit( | ||
currentDateTime.getHours() | ||
)}:${formatTimeUnit(currentDateTime.getMinutes())}:${formatTimeUnit( | ||
currentDateTime.getSeconds() | ||
)}`}</p> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.