Write a program which takes the current time of the system clock, rounds to the nearest minute (you may choose how to round halves) and displays it formatted as the following:

A quarter past one in the morning.
A quarter to two in the afternoon.
Two o'clock in the afternoon.
Twenty-five minutes to three in the morning.
Twenty-seven minutes to seven in the evening.
One minute past twelve in the afternoon.
Twelve o'clock noon.
Twelve o'clock midnight.


  • 12:01am to 11:59am is deemed as morning.
  • 12:01pm to 5:59pm is deemed as afternoon.
  • 6:00pm to 11:59pm is deemed as evening.
  • 15 minutes past the hour must be expressed as A quarter past the hour
  • 30 minutes past the hour must be expressed as Half past the hour
  • 45 minutes past the hour must be expressed as A quarter to the upcoming hour
  • On-the-hour times must be expressed as the hour o'clock, with the addition of noon and midnight being expressed with the addition of noon or midnight respectively.
  • Other times in hh:01 to hh:29 must be expressed as (number of minutes) past the hour.
  • Other times in hh:31 to hh:59 must be expressed as (number of minutes) to the upcoming hour.
  • Twenty-one to twenty-nine must be hyphenated.
  • The first letter of the output must be capitalised. The other letters must be lower-case.

It would be useful to provide a testing version of your code so that it can be tested without manipulating the system clock.

  • 2
    \$\begingroup\$ Let's see what you got... - The specification is still a bit vague. E.g. what about Fifteen minutes past one or Fourty-five minutes past one for the first two examples? \$\endgroup\$
    – Howard
    Commented Dec 10, 2013 at 7:57
  • 1
    \$\begingroup\$ I agree with Howard: there's a lot of dialectal variation here. To ensure that the contest is about golfing you need a complete spec. You also need to address rounding because the time is rarely an integer number of minutes past midnight. \$\endgroup\$ Commented Dec 10, 2013 at 8:39
  • \$\begingroup\$ What? Noon is noon, and midnight is midnight. I suggest those times read "Twelve o'clock noon" and "Twelve o'clock midnight". Noon is not afternoon, or 0=1. \$\endgroup\$
    – boothby
    Commented Dec 10, 2013 at 9:21
  • \$\begingroup\$ Okay, so what do I print at 3:30 PM? Do I have to say "half past three in the afternoon"? \$\endgroup\$
    – Doorknob
    Commented Dec 10, 2013 at 13:15
  • \$\begingroup\$ I'm voting to close until the spec is clarified to avoid more people posting solutions which might have to be withdrawn for not meeting the clarified spec. \$\endgroup\$ Commented Dec 10, 2013 at 14:23

2 Answers 2


Mathematica 450

Date[] calls for the current date and time. [[{4,5}]] returns the hour and minutes

p@h1_:=Which[0<h<12," in the morning.",12<h<18," in the afternoon.",18<h<24, " in the evening."];
s[t_]:=WolframAlpha["spell "<>ToString@t,{{"Result",1},"Plaintext"}];
m==0, Switch[h,12, "noon", 0|24,"midnight",_,s[h~Mod~12]<>p@h],
m==15,"a quarter past "<>w,
m==30,"half past "<>w,
m==45,"a quarter to "<>s[h~Mod~12+1]<>p@h,
m<30,s@m<>" minutes after "<>w,
m>29,s[60-m]<>" minutes before "<>s[h~Mod~12+1]<>p@h]]

Current Time


"ten minutes past three in the afternoon."

Specific times

f[{4, 45}]
f[{10, 30}]
f[{11, 0}]
f[{12, 0}]
f[{13, 15}]
f[{15, 0}]
f[{22, 17}]
f[{24, 0}]

"a quarter to five in the morning."
"half past ten in the morning."
"eleven in the morning."
"a quarter past one in the afternoon."
"three in the afternoon."
"seventeen minutes past ten in the evening."

  • \$\begingroup\$ I noticed I still need to implement "minutes before midnight/noon" and "n minutes after midnight/noon". Will get to that later. \$\endgroup\$
    – DavidC
    Commented Dec 10, 2013 at 20:24
  • \$\begingroup\$ You can save a few characters and get in spec by using "to" rather than "before" and "past" rather than "after". \$\endgroup\$ Commented Dec 12, 2013 at 17:20

Python 2.7, 159 162 156 161

from datetime import*
if n.hour()<12:
elif n.hour()<18:
print"%i in the %s."%(n.hour(),t)

Edit #1 - Added a period at the end of the output.

Edit #2 - Shortened the imports, lengthened code to correct syntax.

  • \$\begingroup\$ Why the verbosity in the print statement? You're calling str on... a string... anyway, much shorter: print"%i in the %s."%(n.hour,t) (and then you can remove the period from the other strings, saving even more chars \$\endgroup\$
    – Doorknob
    Commented Dec 10, 2013 at 13:16
  • 1
    \$\begingroup\$ Attempting to test on ideone gives syntax error. (I was attempted to test because on inspection this doesn't seem to meet the spec, regardless of how the current ambiguities are resolved). \$\endgroup\$ Commented Dec 10, 2013 at 14:22
  • \$\begingroup\$ Would be helpful to show an example with input and output. Looks to me like numbers are rendered as digits rather than letter strings. \$\endgroup\$
    – DavidC
    Commented Dec 10, 2013 at 16:07
  • \$\begingroup\$ @DavidCarraher They are \$\endgroup\$
    – Timtech
    Commented Dec 10, 2013 at 16:15
  • \$\begingroup\$ @Tim, Did you read the spec? It needs to output the time in a written format as I mentioned above... \$\endgroup\$ Commented Dec 10, 2013 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.