Emmanuel Akinfulubi

JavaScriptBasic

Display the current day and time in JavaScript

Beginner · 4 min read

Lots of websites greet you with a line like "Today is Wednesday. Current time is 10 PM : 30 : 38." Dashboards, clocks, chat apps and log files all need to turn a moment in time into words a person can read. In this tutorial you will write a small function that does exactly that.

The function is called dayAndTime. You give it a date, and it gives back one line of text with the name of the day and the time on a 12-hour clock. You only need to know what a function and an array are. Everything about dates is explained as we go.

What a Date gives you

In JavaScript, new Date() creates an object that holds one moment in time: right now. You can also build a fixed moment by passing its parts in order: year, month, day, hours, minutes, seconds.

JavaScript

const d = new Date(2026, 8, 2, 22, 30, 38);
console.log(d.getDay());     // 3
console.log(d.getHours());   // 22
console.log(d.getMinutes()); // 30
console.log(d.getSeconds()); // 38

Two surprises hide in there. The month 8 means September, because months count from 0 (January is 0). And getDay() gives the day of the week as a number from 0 to 6, where 0 is Sunday. So 3 means Wednesday.

getHours() uses the 24-hour clock, so 10 PM comes back as 22. Turning that into "10 PM" is the main job in this exercise.

Why the date is passed in

You could call new Date() inside the function. But then the answer changes every second, and you can never check that it is right. If the date comes in as an argument, you can pass a fixed date and know exactly what should come out. In the real app you simply pass new Date().

Here is the exercise. Try it on your own first if you like: the hints open one at a time. Or read on and we will build it step by step.

1 Today’s day and time, formatted

easy

Produce the current day name and the time in a readable form, like Today is Wednesday. Current time is 10 PM : 30 : 38.

Stuck? Three hints, smallest first
  1. 1. getDay() gives 0 to 6 with 0 meaning Sunday, so index into an array of names.
  2. 2. For 12-hour time, hours % 12 gives 0 for both midnight and noon; fall back to 12.
  3. 3. Pass the date in as an argument rather than calling new Date() inside, or you cannot test it.

Step 1: turn the day number into a name

Put the day names in an array, in the same order JavaScript counts them, starting with Sunday. Then the number from getDay() is the position in the array.

Step 1

const DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const sample = new Date(2026, 8, 2, 22, 30, 38);
console.log(DAYS[sample.getDay()]); // Wednesday

DAYS[3] is the fourth item, 'Wednesday'. If you started the array with Monday, every day would come out one day wrong.

Step 2: AM or PM

Hours from 12 to 23 are afternoon or evening. Anything below 12 is morning. The ? : operator, called the ternary operator, picks one of two values: condition ? valueIfTrue : valueIfFalse.

Step 2

const h24 = 22;
const suffix = h24 >= 12 ? 'PM' : 'AM';
console.log(suffix); // PM

Step 3: convert to the 12-hour clock

The remainder operator % gives what is left over after dividing. 22 % 12 is 10, which is the right 12-hour value. Here is a first try:

JavaScript

const to12 = (h24) => h24 % 12;
console.log(to12(22)); // 10
console.log(to12(0));  // 0, but midnight should read 12
console.log(to12(12)); // 0, but noon should read 12

Midnight and noon both come out as 0. Nobody writes "0 PM". The fix is a fallback: when the remainder is 0, use 12 instead.

Step 3

const to12 = (h24) => (h24 % 12 === 0 ? 12 : h24 % 12);
console.log(to12(0));  // 12
console.log(to12(12)); // 12
console.log(to12(22)); // 10

Step 4: pad the minutes and seconds

A time like "9 : 4 : 5" looks broken. Minutes and seconds should always have two digits. padStart(2, '0') adds zeros to the front of a string until it is 2 characters long. Numbers don't have padStart, so turn the number into a string first with String(n).

Step 4

const pad = (n) => String(n).padStart(2, '0');
console.log(pad(4));  // 04
console.log(pad(38)); // 38

The hour is not padded, which is why the result reads "9 AM" and not "09 AM".

Step 5: put it together

A template literal is a string written between backticks. Anything inside ${ } is worked out and dropped into the text. Now every piece from the steps above goes into one function:

Step 5

const DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

function dayAndTime(date) {
  const day = DAYS[date.getDay()];
  const h24 = date.getHours();
  const suffix = h24 >= 12 ? 'PM' : 'AM';
  const h12 = h24 % 12 === 0 ? 12 : h24 % 12;
  const pad = (n) => String(n).padStart(2, '0');
  return `Today is ${day}. Current time is ${h12} ${suffix} : ${pad(date.getMinutes())} : ${pad(date.getSeconds())}`;
}

console.log(dayAndTime(new Date(2026, 8, 2, 22, 30, 38)));
// Today is Wednesday. Current time is 10 PM : 30 : 38
console.log(dayAndTime(new Date(2026, 8, 2, 9, 4, 5)));
// Today is Wednesday. Current time is 9 AM : 04 : 05

To show the real day and time, call dayAndTime(new Date()).

The full solution

Here is the complete answer. It prints the fixed sample date first, then the real current time, which will be different every time you press Run.

The answer

const DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

// The date is an argument, which is the only way this can be asserted.
function dayAndTime(date) {
  const day = DAYS[date.getDay()];

  const h24 = date.getHours();
  const suffix = h24 >= 12 ? 'PM' : 'AM';
  const h12 = h24 % 12 === 0 ? 12 : h24 % 12;   // 0 and 12 both mean 12

  const pad = (n) => String(n).padStart(2, '0');

  return `Today is ${day}. Current time is ${h12} ${suffix} : ${pad(date.getMinutes())} : ${pad(date.getSeconds())}`;
}

// A fixed date so the output is predictable. Wednesday, 22:30:38.
const sample = new Date(2026, 8, 2, 22, 30, 38);
console.log(dayAndTime(sample));

// And the real use, which cannot be asserted because it changes every second:
console.log('right now:', dayAndTime(new Date()));

Output


      
5 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 5 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

5 checks

test('a full line', dayAndTime(sample), 'Today is Wednesday. Current time is 10 PM : 30 : 38');
test('midnight reads as 12 AM', dayAndTime(new Date(2026, 8, 2, 0, 5, 0)), 'Today is Wednesday. Current time is 12 AM : 05 : 00');
test('noon reads as 12 PM', dayAndTime(new Date(2026, 8, 2, 12, 0, 0)).includes('12 PM'), true);
test('single digits are padded', dayAndTime(new Date(2026, 8, 2, 9, 4, 5)).endsWith('9 AM : 04 : 05'), true);
test('and the day name is indexed by getDay, where Sunday is 0', [new Date(2026, 8, 6).getDay(), dayAndTime(new Date(2026, 8, 6, 1, 2, 3))], [0, 'Today is Sunday. Current time is 1 AM : 02 : 03']);

The two traps are both in the 12-hour conversion. hours % 12 gives 0 at midnight and at noon, so both need the fallback to 12 — the assertions cover each. And taking the date as an argument instead of calling new Date() inside is what makes any of this testable; a function that reads the clock itself can only ever be eyeballed.

Common mistakes

  • Calling new Date() inside the function. It works, but the output changes every second, so there is nothing fixed to check it against.
  • Forgetting midnight and noon. h24 % 12 on its own gives 0 for both. Always test hour 0 and hour 12.
  • Starting the day array with Monday. getDay() counts from Sunday as 0, so a Monday-first array shifts every day by one.

Try it yourself

  1. Add the date to the line: "Today is Wednesday 2 September." Remember that getMonth() counts from 0 and getDate() gives the day of the month.
  2. Write a 24-hour version that returns "22:30:38", with the hour padded too.
  3. Write greeting(date) that returns "Good morning", "Good afternoon" or "Good evening" depending on the hour.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.