25
\$\begingroup\$

Write a program or function with no input that prints or returns this string of 24-hour and 12-hour times:

00:00 12:00am
01:00  1:00am
02:00  2:00am
03:00  3:00am
04:00  4:00am
05:00  5:00am
06:00  6:00am
07:00  7:00am
08:00  8:00am
09:00  9:00am
10:00 10:00am
11:00 11:00am
12:00 12:00pm
13:00  1:00pm
14:00  2:00pm
15:00  3:00pm
16:00  4:00pm
17:00  5:00pm
18:00  6:00pm
19:00  7:00pm
20:00  8:00pm
21:00  9:00pm
22:00 10:00pm
23:00 11:00pm

The string must be output exactly as it appears here. The only exception is that it may optionally have a single trailing newline.

So the MD5 hash of your output should be

827ae6e2dbb1df494930baedb3ee2653

if you do not have a trailing newline and

cd4c3d18abee9bafb495f390a919a13f

if you do. (Your hash could be different if your system uses another type of newline but that's OK.)

The shortest code in bytes wins. Tiebreaker is earlier answer.

\$\endgroup\$
1

36 Answers 36

13
\$\begingroup\$

Bash + coreutils, 43 30

  • Saved 7 bytes thanks to @Yossarian
  • Saved 3 bytes thanks to @AndersKaseorg
seq 0 23|date -f- +%R\ %l:00%P
  • seq generates integers 0-23, one per line.
  • date interprets each line as a datetime. Bare integers appear to be sufficient to be recognised as hours of the day by date. date then outputs each time with the required formatting using the available time format specifiers.

Assumes LANG=C locale, as per this meta answer.

Ideone.

\$\endgroup\$
9
  • 2
    \$\begingroup\$ You can even just do seq 0 23 and save 7 bytes \$\endgroup\$
    – Yossarian
    Commented Jun 1, 2016 at 8:21
  • 1
    \$\begingroup\$ And %H:00%R saves another 3. \$\endgroup\$ Commented Jun 1, 2016 at 11:07
  • \$\begingroup\$ @AndersKaseorg, nop, %R inserts a leading zero, not wanted by the OP. \$\endgroup\$ Commented Jun 1, 2016 at 11:44
  • \$\begingroup\$ @rexkogitans A leading zero is wanted (in the first column, which is the one I’m talking about). \$\endgroup\$ Commented Jun 1, 2016 at 11:54
  • \$\begingroup\$ @AndersKaseorg, oh, you were talking about the first one... then you are right, of course. Corrected my comment above. \$\endgroup\$ Commented Jun 1, 2016 at 12:27
12
\$\begingroup\$

Python 2, 66 bytes

for i in range(24):print'%02d:00%3d:00%sm'%(i,12+i%-12,'ap'[i>11])
\$\endgroup\$
2
  • 1
    \$\begingroup\$ modulo -12 is clever! \$\endgroup\$ Commented May 31, 2016 at 8:00
  • \$\begingroup\$ Just in case anyone is wondering: i=0;exec"print'%02d:00%3d:00%cm'%(i,~-i%12+1,97+i/12*15);i+=1;"*24 changes the code in three different ways, but each change is the same length. \$\endgroup\$
    – Sp3000
    Commented May 31, 2016 at 8:31
8
\$\begingroup\$

C, 73 bytes

m(i){for(i=25;--i;)printf("%02d:00%3d:00%cm\n",24-i,12-i%12,"pa"[i/13]);}

mIllIbyte found a particularly neat way to rewrite this answer. Thanks!

\$\endgroup\$
11
  • \$\begingroup\$ Cool - I never knew about ?: \$\endgroup\$ Commented May 31, 2016 at 17:27
  • \$\begingroup\$ I didn’t know! Interesting. I fixed it :) \$\endgroup\$
    – lynn
    Commented May 31, 2016 at 18:47
  • \$\begingroup\$ Ahh, well, ?: is actually the closest equivalent to Perl’s ||, I believe. C’s || is more like (a || b) ? 1 : 0. \$\endgroup\$
    – lynn
    Commented May 31, 2016 at 20:48
  • 1
    \$\begingroup\$ i++,i%12?:12,"ap"[i/12]) lacks a sequence point. Can't be certain when i++ occurs. Perhaps for(i=0;i<24;i++) ...i,i%12?:12,"ap"[i/12]... \$\endgroup\$
    – chux
    Commented Jun 1, 2016 at 4:26
  • \$\begingroup\$ i++<24 is better - doesn't add more bytes to the source code \$\endgroup\$
    – anatolyg
    Commented Jun 1, 2016 at 7:17
6
\$\begingroup\$

MATL, 46 42 34 bytes

12tEt:qy/t15XObZ"!b16XOhhkw14:X~Z)

Previously, 42 bytes, 12tEt:q2M/736330+t15XObZ"!b16XOhhkw14:X~Z), and 46 bytes, 736330 24t:qw/+t15XO' '24TX"b16XOhhk14: 12X~Z). Of course the 736330 wasn't needed, that was crazy!

Note: Doesn't work with TryItOnline, I think there is a compatibility issue between Matlab and Octaves implementation of datestr.

datestr takes the number representation of a date and converts it to the string representation of that date. The time of the day is the fractional part of the number, so 0.0 corrsponds to January 0, 0000, at time 00:00:00, and 1.0 corresponds to January 1, 0000, at 00:00:00. 1/24 is 1am, 2/24 2am etc.

Explanation

12t         % push a 12 onto the stack and duplicate
Et          % double the 12 and duplicate the 24 (stack now has 12, 24, 24, bottom to top)
:q          % make vector 1:24 and decrement by 1, stack has 12, 24, 0:23
y           % duplicate second element on stack (24)
/           % divide, for (0:23)/24
t           % duplicate elements
15XO        % string representation of date, 15 specifies format
b           % bubble up element in stack (gets a 24 on top of the stack)
Z"!         % makes a column of 24 spaces, to put between columns of times
b           % bubble up another (0:23)/24 
16XO        % string representation of date, 16 for a different format
hh          % concatenate two time vectors and the column of spaces
k           % convert string to lowercase, because CO gives AM/PM not am/pm
w           % swap elements in stack, that first 12 is now on top
14:         % vector of equally spaced values 1:14
X~          % set exclusive-or, returns [1 2 3 4 5 6 7 8 9 10 11 13 14]
Z)          % get the right columns of the string array to remove extra column of blanks
            % implicit display

To show it works in Matlab, here is a screenshot

enter image description here

\$\endgroup\$
0
5
\$\begingroup\$

///, 160 bytes

/Z/:00 //S/Z //A/:00am
//P/:00pm
/00Z12A01S1A02S2A03S3A04S4A05S5A06S6A07S7A08S8A09S9A10Z10A11Z11A12Z12P13S1P14S2P15S3P16S4P17S5P18S6P19S7P20S8P21S9P22Z10P23Z11P

Try it online!

Ungolfed

00:00 12:00am
01:00  1:00am
02:00  2:00am
03:00  3:00am
04:00  4:00am
05:00  5:00am
06:00  6:00am
07:00  7:00am
08:00  8:00am
09:00  9:00am
10:00 10:00am
11:00 11:00am
12:00 12:00pm
13:00  1:00pm
14:00  2:00pm
15:00  3:00pm
16:00  4:00pm
17:00  5:00pm
18:00  6:00pm
19:00  7:00pm
20:00  8:00pm
21:00  9:00pm
22:00 10:00pm
23:00 11:00pm

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Fun fact: making the :00 into a replacement is one byte longer... TIO \$\endgroup\$
    – steenbergh
    Commented Feb 1, 2017 at 9:42
  • \$\begingroup\$ @steenbergh That's because :00 is a 3-byte string that appears 3 times. 3 × 3 = 9, making a replacement in your case costs 3+1+3=7 since it uses a 1-byte alias, and you use it 3 times, so 7+3=10. 9 < 10, so I won't replace it. \$\endgroup\$ Commented Feb 1, 2017 at 11:49
5
\$\begingroup\$

MarioLANG, 965 834 bytes

Try it online

well this was ridiculously complicated.

Technically the output is valid but in practice the Tio for MarioLANG output "n " instead of "n" when we print a number with ':'

if I find the time i'll guess i'll try to do a (probably much longer) version of the program that output correctly on Tio

++<>) +++@++++>   [!) >)>((((::(.)::((.))+:+:--(.)::)).).).)+++++++++
++""+ +"=====""====#) "+"============================================
+++)+ +>>+++++- <+<)->+++ ![-).).).))(::)).(:(:)))..(((::)).(:+(:((((<
+>+++ ++"====<( ")")-"!+++#=========================================="
+(+++>++!++)<+( ++++-+++++>
 -))+)=(#==="+( ++++)+++++"==========================================!
 [!!+-[!(+++!!! !+!<+!++!>(((((+:(.))::(((.
==##===#====###=#=#"=##=#"=================<
++++)))+++++++++++++++(((![-).).).)::)).(:))   >
>========================#==================   "
+>+ >
+"+ "=======================================[   =====================#===============[
+!> ! -).).).))(::)).)):+(..(((::)).(:+(((((<++!-).).).))(::)).)):+(.(((::)).(:+(((((<
=#==#======================================="==#======================================

Explanation :

our main problem here is the fact that we have 6 NaN char (newLine, Space, :, a, p, m)

in marioLANG, in order to print character, we need their ascii value:

  • newLine is 10
  • Space is 32
  • : is 58
  • a is 97
  • p is 112
  • m is 109

So the first thing to do is to set the memory :

++<>) +++@++++>   [!) >)> !
++""+ +"=====""====#) "+"==
+++)+ +>>+++++- <+<)->+++ 
+>+++ ++"====<( ")")-"!+++
+(+++>++!++)<+( ++++-+++++
 -))+)=(#==="+( ++++)+++++
 [!!+-[!(+++!!! !+!<+!++!>
==##===#====###=#=#"=##=#"

with this, the memory look like :

                   v   
  32 58 0 0 97 109 10 0
  _  :      a   m  \n

we will transform a into p during the rest of the program

then we do the actual output :

++<>) +++@++++>   [!) >)>((((::(.)::((.))+:+:--(.)::)).).).)+++++++++
++""+ +"=====""====#) "+"============================================
+++)+ +>>+++++- <+<)->+++ ![-).).).))(::)).(:(:)))..(((::)).(:+(:((((<
+>+++ ++"====<( ")")-"!+++#=========================================="
+(+++>++!++)<+( ++++-+++++>
 -))+)=(#==="+( ++++)+++++"==========================================!
 [!!+-[!(+++!!! !+!<+!++!>(((((+:(.))::(((.
==##===#====###=#=#"=##=#"=================<
++++)))+++++++++++++++(((![-).).).)::)).(:))   >
>========================#==================   "
+>+ >
+"+ "=======================================[   =====================#===============[
+!> ! -).).).))(::)).)):+(..(((::)).(:+(((((<++!-).).).))(::)).)):+(.(((::)).(:+(((((<
=#==#======================================="==#======================================
\$\endgroup\$
4
\$\begingroup\$

Julia, 88 71 66 64 bytes

[@printf "%02d:00%3d:00%cm
" i-11 i%12+1 i>22?112:97for i=11:34]

This is a full program that prints the string with a single trailing newline.

Try it online!

Saved 5 bytes thanks to Sp3000 and 2 thanks to Dennis!

\$\endgroup\$
0
4
\$\begingroup\$

C# function, 100 bytes

void F(){for(int i=0;i<24;i++){Console.Write($"{i:00}:00 {(i+11)%12+1,2}:00 {(i>11?"p":"a")}m\n");}}

Ungolfed version:

void F()
{
    for (int i = 0; i < 24; i++)
    {
        Console.Write($"{i:00}:00 {(i + 11)%12 + 1,2}:00 {(i > 11 ? "p" : "a")}m\n");
    }
}

Console.Write() takes too many characters!

\$\endgroup\$
2
  • \$\begingroup\$ Hello, and welcome to PPCG! I use C# a bit, but what is the leading $ doing to the string? \$\endgroup\$ Commented Jun 1, 2016 at 1:48
  • 3
    \$\begingroup\$ @NoOneIsHere - That's called "string interpolation" and it's a new feature in C# 6.0. You can read more about it here https://msdn.microsoft.com/en-us/library/dn961160.aspx. \$\endgroup\$
    – STLDev
    Commented Jun 1, 2016 at 3:15
3
\$\begingroup\$

JavaScript (ES2015), 147 138 137 134 133 bytes

((o,x,r)=>{for(i=0;i<24;)b=i%12,c=b||12,o+='0'[r](i<10)+i+++x+' '[r]((c<10)+1)+c+x+(i<13?'a':'p')+"m\n";return o})('',':00','repeat')

In this version I took advantage of String.repeat() method to get rid of lengthy .slice() and .join() and moved incrementation inside loop.

Previous version:

((o,x,i)=>{for(;i<24;i++){b=i%12;o+=[`0${i+x}`.slice(-5),(b||12)+x+(i<12?'a':'p')+'m'].join(' '.repeat((b>0&&b<10)+1))+"\n"}return o})('',':00',0)

Gives output with trailing newline. Tested in Firefox Scratchpad. Not sure if passing arguments to IIFE is OK with "no input" rule.

It's my first submission, so hello to everyone! :)

\$\endgroup\$
3
\$\begingroup\$

TSQL(SQLServer 2012) 146 124 121

DECLARE @ DATETIME=0WHILE @<1BEGIN PRINT
CONVERT(char(5),@,108)+' '+LOWER(RIGHT(FORMAT(@,'g'),8))SET @=dateadd(hh,1,@)END

Try it online!

First attempt, a bit longer, but a one-liner:

SELECT CONVERT(char(5),n,108)+' '+LOWER(RIGHT(FORMAT(n,'g'),8))FROM(SELECT
top 24 dateadd(hh,Number,0)n FROM master..spt_values WHERE'P'=type)x

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Reduced to 121 chars: try it online \$\endgroup\$ Commented Jun 14, 2016 at 14:58
  • \$\begingroup\$ @RossPresser once again you helped me, learning alot here \$\endgroup\$ Commented Jun 14, 2016 at 15:06
2
\$\begingroup\$

Perl 5, 58

map{printf"%02u:00%3u:00%sm
",$_,$_%12||12,$_>11?p:a}0..23
\$\endgroup\$
2
\$\begingroup\$

Javascript, 122 Bytes, 120 Bytes

f=j=>j>12?j-12:j;for(i=0;i<24;i++)console.log('%s:00 %s:00%s',i<10?'0'+i:i,i==0?12:f(i)<10?' '+f(i):f(i),i>11?'pm':'am')

Edit: Small bug fixed + output:

00:00 12:00am
01:00  1:00am
02:00  2:00am
03:00  3:00am
04:00  4:00am
05:00  5:00am
06:00  6:00am
07:00  7:00am
08:00  8:00am
09:00  9:00am
10:00 10:00am
11:00 11:00am
12:00 12:00pm
13:00  1:00pm
14:00  2:00pm
15:00  3:00pm
16:00  4:00pm
17:00  5:00pm
18:00  6:00pm
19:00  7:00pm
20:00  8:00pm
21:00  9:00pm
22:00 10:00pm
23:00 11:00pm
\$\endgroup\$
1
2
\$\begingroup\$

V, 56 53 bytes

i00:00 23ñYpñH12G$yP13G$pgvó $/am
í/pm
í 0/  
í/12

Try it online!

Since this can be hard to enter, here is a reversible hexdump:

00000000: 6930 303a 3030 201b 3233 f159 7001 f148  i00:00 .23.Yp..H
00000010: 1631 3247 2479 5031 3347 2470 6776 f320  .12G$yP13G$pgv. 
00000020: 242f 616d 0aed 2f70 6d0a ed20 302f 2020  $/am../pm.. 0/  
00000030: 0aed 2f31 320a                           ../12.

A noncompeting version is trivially 2 bytes shorter if you replace both occurrences of G$ with L, which was supposed to be the same but had a bug.

Explanation:

i00:00<esc>                                     #Enter the starting text.
           23ñYp<C-a>ñ                          #Duplicate and increment 23 times
                      H                         #Move back to the beginning
                       <C-v>12G$y               #Select 12 lines horizontally
                                 P              #Horizontally paste
                                  13G$p         #Move to line 13 and Horizontally paste again
                                       gv       #Reselect the top 12 lines
                                         ó $/am #Replace a space at the end of the line with 'am'

í/pm      #Replace the previous search with 'pm'
í 0/      #Replace "Space+0" with 2 spaces
í/12      #Replace the previous search with "12"
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 51 50 48 44 42 bytes

Saved two bytes thanks to carusocomputing

Code:

24FNgi0}N…:00©ðN12(%12+Dgiðs}®„paN12‹è'mJ,

Try it online!

Explanation

24F                                         # for N in [0...23]
   Ngi0}                                    # if len(N)=1, push 0
        N                                   # push N
         …:00©                              # push ":00" and store a copy in register
             ð                              # push " "
              N12(%12+D                     # push 2 copies of N%(-12)+12
                       giðs}                # if the length of that number is 1, 
                                            # push " " and swap with the number
                            ®               # push ":00" again
                             „pa            # push "pa"
                                N12‹è       # index into that with N<12
                                     'm     # push "m"
                                       J,   # join everything and print with newline
\$\endgroup\$
3
  • \$\begingroup\$ codegolf.stackexchange.com/questions/103242/… ;). Now whether or not that could be optimized below 50 is up for question haha. \$\endgroup\$ Commented Jan 31, 2017 at 21:14
  • \$\begingroup\$ 23Ý instead of 24L< for 1 byte. And how long has ë existed? I feel so dumb not knowing about else statements in 05AB1E until now. \$\endgroup\$ Commented Jan 31, 2017 at 21:18
  • \$\begingroup\$ @carusocomputing: Thanks! else has existed for a while, but it's been buggy at times. Especially when nesting ifs. \$\endgroup\$
    – Emigna
    Commented Jan 31, 2017 at 21:22
2
\$\begingroup\$

Vyxal 3 RṀj, 38 bytes

24ƛ2Þ0m12:£N%¥+ᶲ;2«":00"+„ᶴpan¥-„i+'m+

Try it Online!

Port of 05ab1e

my own 44 byter

\$\endgroup\$
1
\$\begingroup\$

PowerShell v2+, 76 bytes

0..23|%{"{0:D2}:00{1,3}:00"-f$_,(($_%12),12)[!($_%12)]+('am','pm')[$_-ge12]}

Loops from 0..23 and each loop sets a string with the -f operator. The first {0:D2} ensures we have prepended zeros, the second {1,3} ensures we have padded spaces for the middle column. The {0} one corresponds to the $_ of the -f operator, while the {1} corresponds to the pseudo-ternary that chooses between $_%12 or 12 based on whether $_%12 is non-zero or not (i.e., if we're at $_=13, this will choose 1 for 1:00pm). We then concatenate that with another pseudo-ternary that chooses the appropriate am/pm.


As opposed to my answer on List all times in the day at a half hour rate, it's actually shorter here to brute-force the numbers since we get significantly cheaper padding. Here's the answer using date functions, at 78 bytes

0..23|%{(Date -h $_ -f 'HH:00')+(Date -h $_ -f "h:00tt").ToLower().PadLeft(8)}
\$\endgroup\$
1
\$\begingroup\$

C++, 81 79 bytes

[]{for(time_t t=0,y;t<24;cout<<put_time(gmtime(&y),"%R %l:00%P\n"))y=t++*3600;}

This code requires using namespace std somewhere preceding it.

It does a loop on the values 0...23. It multiplies each value by 3600, converts to a tm struct and prints it. The print format %R outputs the 24-hour and minute; the print formats %l and %P output the proper 12-hour parts; they require GNU.

A working online version is here.

\$\endgroup\$
1
\$\begingroup\$

Ruby, 66 62 bytes

0.upto(23){|i| puts "%02d:00%3d:00#{'ap'[i/12]}m"%[i,(i-1)%12+1]}

New version

24.times{|i|puts"%02d:00%3d:00#{'ap'[i/12]}m"%[i,(i-1)%12+1]}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 24.times is shorter. No need for the spaces around puts. \$\endgroup\$
    – manatwork
    Commented Jun 1, 2016 at 12:05
  • \$\begingroup\$ You can replace (i-1) with ~-i for 2 bytes. \$\endgroup\$
    – Jordan
    Commented Nov 7, 2017 at 18:55
1
\$\begingroup\$

JavaScript (ES6), 119 116 bytes

_=>Array(24).fill().map((_,i)=>`${`0${i}`.slice(-2)}:00 ${` ${(i+11)%12+1}`.slice(-2)}:00${'ap'[+(i>11)]}m`).join`
`
\$\endgroup\$
1
\$\begingroup\$

Sclipting, 76 bytes

The program assumes that the input is empty (or '0' or anything that converts to the integer 0).

The byte count assumes UTF-16 encoding.

군上❶겠小꼀虛嗎❷꾣갰글❷결加곀剩增❶겠小글虛嗎댆밁⓷꾣갰⓷⓼곀小掘닐밊終

Ungolfed:

군 // 23
上 // for loop (goes from 0 to 23 if input is 0)
    ❶겠小꼀虛嗎 // n < 10 ? "0" : ""
    ❷          // n
    꾣갰글      // ":00 "
    ❷결加곀剩增 // k = (n+11) % 12 + 1
    ❶겠小글虛嗎 // k < 10 ? " " : ""
    댆밁       // "pa"
    ⓷         // Pull n to top of stack
    꾣갰       // ":00"
    ⓷         // Pull "pa" to top of stack
    ⓼         // Pull k to top of stack
    곀小掘     // "pa"[k < 10 ? 1 : 0]
    닐밊       // "m\n"
終 // end of for loop

Each iteration of the loop leaves lots of small strings on the stack; at the end they are all automatically concatenated.

\$\endgroup\$
1
\$\begingroup\$

JavaScript, 97 95 bytes

This is based off of starcorder’s answer. Thanks to George Reith for a 2 byte improvement.

for(i=0,k=12;i<24;k=i++%12+1)console.log('%s:00 %s:00%sm',i>9?i:'0'+i,k>9?k:' '+k,i>11?'p':'a')

Ungolfed:

for (i=0, k=12; i < 24; k = (i++) % 12 + 1)
    console.log('%s:00 %s:00%sm',
        i > 9 ? i : '0' + i,
        k > 9 ? k : ' ' + k,
        i > 11 ? 'p' : 'a')
\$\endgroup\$
1
  • \$\begingroup\$ You can save 2 bytes by rewriting n < 10 as 9 < n and swapping the ternary cases \$\endgroup\$ Commented Jul 13, 2016 at 11:40
1
\$\begingroup\$

Batch, 167 bytes

@echo off
set h=11
set p=a
for /l %%a in (0,1,23)do call:e %%a
exit/b
:e
set a=0%1
set/ah=h%%12+1
set h= %h%
if %1==12 set p=p
echo %a:~-2:00 %h:~-2%:00%p%m
\$\endgroup\$
1
\$\begingroup\$

VBA, 91 Bytes

Anonymous VBE immediate window function that outputs to the VBE immediate window.

For i=0To 23:t=TimeSerial(i,0,0):?Format(t,"hh:mm")Right("  "+Format(t,"h:mmam/pm"),8):Next
\$\endgroup\$
4
  • \$\begingroup\$ Hi Sir, you can save 8 bytes by replacing Right(" "+Format(t,"h:mmam/pm"),8) with &" "&format(t,"h:mmam/pm") :) \$\endgroup\$
    – remoel
    Commented Nov 6, 2017 at 10:53
  • \$\begingroup\$ @remoel - unfortunately the correction you have purposed is not valid as it does not account for the double spacing on hours that have a single digit hour value - and thus does not produce the correct MD5 hash \$\endgroup\$ Commented Nov 6, 2017 at 18:20
  • \$\begingroup\$ ah Yes. I'm sorry. I forgot about the md5. my bad. Just one more thing, your code output 12am-12am not 12am-11pm. :3 \$\endgroup\$
    – remoel
    Commented Nov 7, 2017 at 6:49
  • \$\begingroup\$ @remoel, ahh - you are correct; and I have fixed this \$\endgroup\$ Commented Nov 7, 2017 at 17:23
1
\$\begingroup\$

Kotlin, 95 bytes

It can be improved for sure.

fun p(){for(i in 0..23)println("%02d:00 ${(i+11)%12+1}:00${if(i>12)"p" else "a"}m".format(i))}
\$\endgroup\$
1
\$\begingroup\$

Go 1.22+, 134 bytes

import(."fmt";."time")
func f(){for i:=range 24{d:=Date(0,1,1,i,0,0,0,UTC)
Printf("%s % 7s\n",d.Format("15:00"),d.Format("3:00pm"))
}}

Attempt This Online!

Uses fmt.Printf and time.Time.Format in a pretty straightforward manner.

Prints with trailing newline.

Go, 136 bytes

import(."fmt";."time")
func f(){for i:=0;i<24;i++{d:=Date(0,1,1,i,0,0,0,UTC)
Printf("%s % 7s\n",d.Format("15:00"),d.Format("3:00pm"))
}}

Attempt This Online!

\$\endgroup\$
0
\$\begingroup\$

PHP, 110 107 bytes

for($h=0;$h<24;){$m=($h+11)%12+1;echo($h<10?0:"")."$h:00 ".($m<10?" ":"")."$m:00".($h++<12?"a":"p")."m\n";}
exploded view
for ($h=0; $h<24; ) {
  $m = ($h+11) % 12 + 1;
  echo ($h < 10 ?  0  : "") . "$h:00 " .
       ($m < 10 ? " " : "") . "$m:00"  . ($h++ < 12 ? "a" : "p") . "m\n";
}

Somewhat surprised, tried to turn the ($i < 10 ? $s : "") . "$i:00" bit into a function, but it wound up adding ~25 characters. No go there.

\$\endgroup\$
0
\$\begingroup\$

Swift, 85 bytes

for x in 0...23{print(String(format:"%02d:00 %2d:00\(x<12 ?"a":"p")m",x,12+x % -12))}
\$\endgroup\$
2
  • \$\begingroup\$ This doesn't work, at least not for me on Swift 2.2. Output \$\endgroup\$
    – JAL
    Commented Jun 1, 2016 at 22:14
  • \$\begingroup\$ I had to change your format string: String(format: "%02d:00 %2d:00\(x<12 ?"a":"p")m", x, x%12 != 0 ? x%12 : 12) \$\endgroup\$
    – JAL
    Commented Jun 1, 2016 at 22:15
0
\$\begingroup\$

C Function, 82 bytes

m(i){for(;i<24;printf("%02d:00 %2d:00%cm\n",i,i%12==0?12:i%12,i>11?'p':'a'),i++);}

Usage, 94 Byte

m(i){for(;i<24;printf("%02d:00 %2d:00%cm\n",i,i%12==0?12:i%12,i>11?'p':'a'),i++);}main(){m();}

Ungolfed, 337 Bytes

#include <stdio.h>
void m(){
    int i,a;
    char c;
    for(i=0;i<24;i++){
        if (i%12==0){
            a = 12;
        }
        else{
            a = i%12;
        }
        if (i>11){
            c = 'p';
        } else{
            c = 'a';
        }
        printf("%02d:00 %2d:00%cm\n",i,a,c);
    }
}
int main(){
    m();
}

it works on Windows:

in the warning you can find the whole program

C Program, 85 bytes

main(i){for(;i<24;printf("%02d:00 %2d:00%cm\n",i,i%12==0?12:i%12,i>11?'p':'a'),i++);}
\$\endgroup\$
6
  • \$\begingroup\$ You might need initialization i=0 for the function version. And for the program version, you can stuff the code directly into main - no need to define a function there! \$\endgroup\$
    – anatolyg
    Commented May 31, 2016 at 16:44
  • \$\begingroup\$ Thanks! edited!! FYI: In the funcion version if you use it without params it's self initialized to 0! \$\endgroup\$ Commented May 31, 2016 at 16:53
  • \$\begingroup\$ i%12==0?12:i%12 --> i%12?i%12:12 \$\endgroup\$
    – chux
    Commented Jun 1, 2016 at 11:54
  • \$\begingroup\$ "In the funcion version if you use it without params it's self initialized to 0!" This does not appear to be standard C. Any reference to support this? \$\endgroup\$
    – chux
    Commented Jun 1, 2016 at 11:57
  • \$\begingroup\$ i have noticed it works only on the gcc i have installed on my windows system.. a friend of mine with linux told me it doesn't work on his pc but i don't know how to fix it for linux... \$\endgroup\$ Commented Jun 1, 2016 at 12:51
0
\$\begingroup\$

Foo, 163 bytes

Pretty brute-force approach; nothing clever here (I tried in a couple spots but it ended up being shorter not to), just wanted to give Foo a shot. Foo automatically prints anything within quotes. $c10 prints a line break. (## ... ) loops until the current cell equals ##.

"00:00 12:00am"$c10+1(10"0"$i":00  "$i":00am"$c10+1)(12$i":00 "$i":00am"$c10+1)"12:00 12:00pm"$c10+1(22$i":00  ">+1$i<":00pm"$c10+1)(24$i":00 ">+1$i<":00pm"$c10+1)

Ungolfed a bit:

"00:00 12:00am"$c10+1
(10"0"$i":00  "$i":00am"$c10+1)
(12$i":00 "$i":00am"$c10+1)
"12:00 12:00pm"$c10+1
(22$i":00  ">+1$i<":00pm"$c10+1)
(24$i":00 ">+1$i<":00pm"$c10+1)

Try it online

\$\endgroup\$
0
\$\begingroup\$

Javascript (using external library - Enumerable) (107 bytes)

_.Range(0,24).WriteLine(x=>((x<10?"0"+x:x)+":00 "+(((h=((x+11)%12)+1))<10?" "+h:h)+":00"+(x<12?"am":"pm")))

Link to library: https://github.com/mvegh1/Enumerable/

Code explanation: Create array of integers from 0 to 23, for each write a line according to the predicate. That predicate checks if current val is less than 10, and pads it with 0, else uses the current val as is. Then adds the minutes string to it. Then basically does a little trickery to convert military to am/pm time, and handles padding for am/pm times less than 10.

enter image description here

\$\endgroup\$

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.