Skip to content

Instantly share code, notes, and snippets.

@Steellgold
Created May 4, 2024 18:18
Show Gist options
  • Save Steellgold/de61bda61e10233fe0bfae5968bebe12 to your computer and use it in GitHub Desktop.
Save Steellgold/de61bda61e10233fe0bfae5968bebe12 to your computer and use it in GitHub Desktop.
The addZero function is used to format numbers by adding a zero in front of those less than 10, thereby enhancing the visual presentation of digital displays, such as times or dates, to make them more aesthetically pleasing and consistent.
import { ReactElement } from "react";
export const TimeDisplay = (): ReactElement => {
const currentHour = new Date().getHours();
const currentMinute = new Date().getMinutes();
const formattedHour = addZero(currentHour);
const formattedMinute = addZero(currentMinute);
return <p>Current Time: {formattedHour}:{formattedMinute}</p>;
}
const addZero = (i: number) => {
return i < 10 ? `0${i}` : i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment