- Create a Date:
const d = new Date(); - Parts:
- Month (0--11) →
d.getMonth() + 1\ - Day (1--31) →
d.getDate()\ - Year (YYYY) →
d.getFullYear()\ - Day of week (0--6, Sun--Sat) →
d.getDay()
- Month (0--11) →
- Epoch ms:
d.getTime()(ms since Jan 1, 1970) - Timer loops:
- Start:
const id = setInterval(fn, 10);\ - Stop:
clearInterval(id);
- Start:
- Writing to page:
document.getElementById('someId').textContent = '…'; - Event listeners (required):
document.getElementById('myBtn').addEventListener('click', handler);
(No inlineonclickattributes.)
Build a small page that:
- Shows the current date as:
DayOfWeek, M/D/YYYY(e.g.,Tuesday, 10/28/2025) inside#myDate - Starts a running elapsed time (seconds) since page load,
updating ~every 10ms, inside
#myTimer - Toggles the timer Start/Stop with a button
#myBtn(text switches between "Stop" and "Start")
myDate--- where you print the date stringmyTimer--- where you print elapsed seconds (e.g.,1.23)myBtn--- the toggle button (must be wired withaddEventListener, not inlineonclick)
- On load, display the date in
#myDate. - Also on load, capture a
start = new Date().getTime()and begin asetIntervalthat:- Reads
now = new Date().getTime() - Computes elapsed seconds:
(now - start) / 1000 - Writes that to
#myTimerwith two decimals (e.g.,time.toFixed(2)).
- Reads
- The button toggles:
- If running →
clearInterval, set text to Start - If stopped →
setIntervalagain, set text to Stop
- If running →
Tip: Map
getDay()to names:['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'][d.getDay()].
<div id="myDate"></div>
<div id="myTimer"></div>
<button id="myBtn">Stop</button>- Required elements exist (
#myDate,#myTimer,#myBtn) - Event wiring uses
addEventListener(no inlineonclick) - Date format looks like
DayOfWeek, M/D/YYYYand the numeric date matches today - Timer runs (value increases after ~120ms)
- Toggle works (button text flips Start/Stop and timer pauses/resumes)
- Elapsed is monotonic (keeps increasing while running)
Score: 100 points total (10 + 15 + 20 + 20 + 20 + 15)