24 and 12 Hour Times

24

6

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.

Calvin's Hobbies

Posted 2016-05-31T03:38:03.137

Reputation: 84 000

Answers

13

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.

Digital Trauma

Posted 2016-05-31T03:38:03.137

Reputation: 64 644

2You can even just do seq 0 23 and save 7 bytes – Yossarian – 2016-06-01T08:21:58.990

1And %H:00%R saves another 3. – Anders Kaseorg – 2016-06-01T11:07:47.613

@AndersKaseorg, nop, %R inserts a leading zero, not wanted by the OP. – rexkogitans – 2016-06-01T11:44:05.903

@rexkogitans A leading zero is wanted (in the first column, which is the one I’m talking about). – Anders Kaseorg – 2016-06-01T11:54:25.220

@AndersKaseorg, oh, you were talking about the first one... then you are right, of course. Corrected my comment above. – rexkogitans – 2016-06-01T12:27:53.367

For me, %P does not output anything (German locale), so seq 0 23|LANG=C date -f- +%R\ %l:00%P is correct, 37 Bytes – rexkogitans – 2016-06-01T12:30:17.150

@Yossarian Excellent - thanks! – Digital Trauma – 2016-06-01T15:32:04.940

@AndersKaseorg Very good - thanks! – Digital Trauma – 2016-06-01T15:32:15.080

@rexkogitans I think LANG=C may be used as a default, though there is no harm in explicitly stating this - see my edit. – Digital Trauma – 2016-06-01T15:33:59.687

12

Python 2, 66 bytes

for i in range(24):print'%02d:00%3d:00%sm'%(i,12+i%-12,'ap'[i>11])

kennytm

Posted 2016-05-31T03:38:03.137

Reputation: 6 847

1modulo -12 is clever! – Erik the Outgolfer – 2016-05-31T08:00:40.570

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. – Sp3000 – 2016-05-31T08:31:27.253

8

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!

Lynn

Posted 2016-05-31T03:38:03.137

Reputation: 55 648

Cool - I never knew about ?: – Digital Trauma – 2016-05-31T17:27:52.590

I didn’t know! Interesting. I fixed it :) – Lynn – 2016-05-31T18:47:13.850

Ahh, well, ?: is actually the closest equivalent to Perl’s ||, I believe. C’s || is more like (a || b) ? 1 : 0. – Lynn – 2016-05-31T20:48:10.767

1i++,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]... – chux - Reinstate Monica – 2016-06-01T04:26:51.520

i++<24 is better - doesn't add more bytes to the source code – anatolyg – 2016-06-01T07:17:22.630

(i+11)%12+1 is equivalent to (i-1)%12+1 (saved 1 byte) – aross – 2016-06-01T11:11:37.463

@anatolyg for(i=0;i++<24;) does not print 00:00 12:00am – chux - Reinstate Monica – 2016-06-01T11:46:48.110

@chux for(i=-1;i++<24;) is one way to solve it – aross – 2016-06-01T12:01:22.663

It seems you cannot get rid of the reliance on the specifics of undefined behavior without increasing code size. This is so 1337! – anatolyg – 2016-06-01T14:41:41.590

How about m(i){for(i=25;--i;)printf("%02d:00%3d:00%cm\n",24-i,12-i%12,"pa"[i/13]);} . Although, I am a fan of the Elvis operator. ?:) – mIllIbyte – 2016-06-02T19:18:42.770

@mIllIbyte I love it!! – Lynn – 2016-06-02T19:25:07.400

6

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

David

Posted 2016-05-31T03:38:03.137

Reputation: 1 316

5

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 :

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

Ether Frog

Posted 2016-05-31T03:38:03.137

Reputation: 343

5

///, 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

Erik the Outgolfer

Posted 2016-05-31T03:38:03.137

Reputation: 38 134

Fun fact: making the :00 into a replacement is one byte longer... TIO

– steenbergh – 2017-02-01T09:42:11.193

@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. – Erik the Outgolfer – 2017-02-01T11:49:34.720

4

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!

Alex A.

Posted 2016-05-31T03:38:03.137

Reputation: 23 761

4

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!

STLDev

Posted 2016-05-31T03:38:03.137

Reputation: 141

Hello, and welcome to PPCG! I use C# a bit, but what is the leading $ doing to the string? – NoOneIsHere – 2016-06-01T01:48:11.153

3

@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.

– STLDev – 2016-06-01T03:15:38.950

3

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!

t-clausen.dk

Posted 2016-05-31T03:38:03.137

Reputation: 2 874

1

Reduced to 121 chars: try it online

– Ross Presser – 2016-06-14T14:58:59.340

@RossPresser once again you helped me, learning alot here – t-clausen.dk – 2016-06-14T15:06:16.767

3

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! :)

Leibrug

Posted 2016-05-31T03:38:03.137

Reputation: 121

2

Perl 5, 58

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

msh210

Posted 2016-05-31T03:38:03.137

Reputation: 3 094

2

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"

James

Posted 2016-05-31T03:38:03.137

Reputation: 54 537

2

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

starcorder

Posted 2016-05-31T03:38:03.137

Reputation: 461

4In case you're wondering about the downvote. – Dennis – 2016-05-31T16:17:48.343

2

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

Emigna

Posted 2016-05-31T03:38:03.137

Reputation: 50 798

http://codegolf.stackexchange.com/questions/103242/bridges-are-metaphors-for-everything-in-ascii-art/103264#103264 ;). Now whether or not that could be optimized below 50 is up for question haha. – Magic Octopus Urn – 2017-01-31T21:14:08.923

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. – Magic Octopus Urn – 2017-01-31T21:18:34.837

@carusocomputing: Thanks! else has existed for a while, but it's been buggy at times. Especially when nesting ifs. – Emigna – 2017-01-31T21:22:47.373

1

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

Neil

Posted 2016-05-31T03:38:03.137

Reputation: 95 035

1

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)}

AdmBorkBork

Posted 2016-05-31T03:38:03.137

Reputation: 41 581

1

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.

anatolyg

Posted 2016-05-31T03:38:03.137

Reputation: 10 719

1

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))}

Rames

Posted 2016-05-31T03:38:03.137

Reputation: 221

1

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]}

Gosha U.

Posted 2016-05-31T03:38:03.137

Reputation: 129

124.times is shorter. No need for the spaces around puts. – manatwork – 2016-06-01T12:05:42.803

You can replace (i-1) with ~-i for 2 bytes. – Jordan – 2017-11-07T18:55:09.720

1

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`
`

ASCII-only

Posted 2016-05-31T03:38:03.137

Reputation: 4 687

1

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')

Timwi

Posted 2016-05-31T03:38:03.137

Reputation: 12 158

You can save 2 bytes by rewriting n < 10 as 9 < n and swapping the ternary cases – George Reith – 2016-07-13T11:40:44.917

1

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.

Timwi

Posted 2016-05-31T03:38:03.137

Reputation: 12 158

0

SmileBASIC, 73 bytes

FOR H=0TO 23?FORMAT$(%02D:00 %2D:00%Sm",H,(H+11)MOD 12+1,"ap"[H>11])NEXT

Someone found a better 24->12 hour formula than my old one, which saves 3 bytes, and 5 bytes in another program

12Me21

Posted 2016-05-31T03:38:03.137

Reputation: 6 110

0

tcl, 93

set i 0;time {puts [format %02d:00%3d:00[expr $i<12?"a":"p"]m $i [expr $i%-12+12]];incr i} 24

demo

sergiol

Posted 2016-05-31T03:38:03.137

Reputation: 3 055

0

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

Taylor Scott

Posted 2016-05-31T03:38:03.137

Reputation: 6 709

Hi Sir, you can save 8 bytes by replacing Right(" "+Format(t,"h:mmam/pm"),8) with &" "&format(t,"h:mmam/pm") :) – remoel – 2017-11-06T10:53:27.670

@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 – Taylor Scott – 2017-11-06T18:20:16.120

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 – remoel – 2017-11-07T06:49:16.827

@remoel, ahh - you are correct; and I have fixed this – Taylor Scott – 2017-11-07T17:23:34.727

0

Groovy, 90/94 bytes

90 byte version (only works in a script):

24.times{a->s=' ';m=a%12?:12;println"${a<10?0:''}$a:00 ${m<10?s:''}$m:00${a<12?'a':'p'}m"}

94 byte version:

24.times{a->def s=' ',m=a%12?:12;println"${a<10?0:''}$a:00 ${m<10?s:''}$m:00${a<12?'a':'p'}m"}

Hlaaftana

Posted 2016-05-31T03:38:03.137

Reputation: 41

0

LOLCODE, 365 bytes

I'll golf down...

HAI 1.3
VISIBLE "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 1:00pm"
KTHXBYE

Try it online!

FantaC

Posted 2016-05-31T03:38:03.137

Reputation: 1 425

0

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++);}

Giacomo Garabello

Posted 2016-05-31T03:38:03.137

Reputation: 1 419

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! – anatolyg – 2016-05-31T16:44:42.830

Thanks! edited!! FYI: In the funcion version if you use it without params it's self initialized to 0! – Giacomo Garabello – 2016-05-31T16:53:10.593

i%12==0?12:i%12 --> i%12?i%12:12 – chux - Reinstate Monica – 2016-06-01T11:54:30.680

"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? – chux - Reinstate Monica – 2016-06-01T11:57:31.833

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... – Giacomo Garabello – 2016-06-01T12:51:27.520

the command gcc --version output this : gcc (GCC) 4.8.1 Copyright (C) 2013 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. – Giacomo Garabello – 2016-06-01T12:58:53.133

0

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.

ricdesi

Posted 2016-05-31T03:38:03.137

Reputation: 499

0

Swift, 85 bytes

for x in 0...23{print(String(format:"%02d:00 %2d:00\(x<12 ?"a":"p")m",x,12+x % -12))}

GoatInTheMachine

Posted 2016-05-31T03:38:03.137

Reputation: 463

This doesn't work, at least not for me on Swift 2.2. Output

– JAL – 2016-06-01T22:14:55.293

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) – JAL – 2016-06-01T22:15:23.480

0

PHP, 67 65 64 bytes

This uses IBM-850 encoding.

for(;$i<24;)printf(~┌¤═ø┼¤¤┌╠ø┼¤¤┌îƧ,$i,$i%12?:12,$i++>11?p:a);

With the unencoded string (66 bytes):

for(;$i<24;)printf("%02d:00%3d:00%sm\n",$i,$i%12?:12,$i++>11?p:a);

Run like this:

php -n -r 'for(;$i<24;)printf(~┌¤═ø┼¤¤┌╠ø┼¤¤┌îƧ,$i,$i%12?:12,$i++>11?p:a);'

Tweaks

  • Saved 2 bytes by improving sprintf format
  • Saved a byte by getting rid of unnecessary space (thx @Titus)

aross

Posted 2016-05-31T03:38:03.137

Reputation: 1 583

You can use -n instead of -d error_reporting=30709. There should be no space before am/pm. Save one byte on the unencoded version with a physical linebreak. – Titus – 2017-02-01T20:33:58.947

0

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

SnoringFrog

Posted 2016-05-31T03:38:03.137

Reputation: 1 709

0

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

applejacks01

Posted 2016-05-31T03:38:03.137

Reputation: 989

-1

C#, 91 bytes

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

AntonB

Posted 2016-05-31T03:38:03.137

Reputation: 99

hmmm... for values of I less than 10, this doesn't print a leading 0 for the hour of the military time. – STLDev – 2016-06-01T00:24:30.543

good call, should display it properly now – AntonB – 2016-06-02T02:06:22.703