Skip to solution
easyMachine Coding

Build a Digital Clock

277 views
01

Understand the problem

Build a live digital clock with 12/24-hour toggle.

ReactTimersState
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Solve in

To build a digital clock in React, the core idea is to use useState to hold the current time and useEffect with setInterval to update it every second. We'll manage the 12/24-hour format with another useState and format the time accordingly.

Step 1 — Initialize State for Time and Format

Start by setting up

Solution ready — 2 min read

Classified // press E to declassify

04

React solution

Complete solutionRun Playground
import React from 'react';
import DigitalClock from './src/DigitalClock';

export default function App() {
  return (
    <div style={{ fontFamily: 'sans-serif', textAlign: 'center', padding: '20px' }}>
      <h1>React Digital Clock</h1>
      <p>A live clock with 12/24-hour format toggle.</p>
      <DigitalClock />
      <p style={{ marginTop: '30px', fontSize: '0.8em', color: '#666' }}>
        The clock above updates every second.
      </p>
    </div>
  );
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 17 of 87 decoded in the Frontend Machine Coding track. One more won't hurt.

Back to track