10
\$\begingroup\$

Challenge

Convert and print out a time in a 12-hour format. HH:MM AM/PM

Examples

Input:

  • 'Fri Jun 30 2017 21:14:20 GMT-0700 (PDT)'
  • 'Fri Jun 30 2017 00:10:23 GMT-0700 (PDT)'
  • 'Fri Jun 30 2017 12:10:23 GMT-0700 (PDT)'
  • 'Sat Jun 31 2018 8:06:20 GMT-0700 (PDT)'
  • 'Fri Jul 01 2017 01:14:20 GMT-0700 (PDT)'
  • 'Sat Apr 10 2020 09:06:20 GMT-0700 (PDT)'

Ouput:

  • 9:14 PM
  • 12:10 AM
  • 12:10 PM
  • 08:06 AM
  • 1:14 AM
  • 09:06 AM

Fine Points

  • A zero before a one digit number is okay, no zero is also allowed. The following examples are both allowed:

    • 9:06 AM

    • 09:06 AM

  • All tested years will be after 999 (each year will be exactly 4 digits)

Rules

\$\endgroup\$
  • \$\begingroup\$ Suggested test cases: 00:10:23 --> 12:10 AM and 12:10:23 --> 12:10 PM. \$\endgroup\$ – Arnauld Jul 3 '17 at 1:23
  • \$\begingroup\$ there we go. Feel free to edit if you see anything else I should change. thanks for the suggestion! \$\endgroup\$ – zoecarver Jul 3 '17 at 1:25
  • \$\begingroup\$ Related \$\endgroup\$ – Not a tree Jul 3 '17 at 1:34
  • 2
    \$\begingroup\$ May we assume that the time is given in the timezone the program is being run in? (e.g. 'Fri Jun 30 2017 21:14:20 GMT-0400 (EDT)' for me) \$\endgroup\$ – ETHproductions Jul 3 '17 at 1:37
  • 9
    \$\begingroup\$ erm, June 31 doesn't exist. Is that accurate? \$\endgroup\$ – Eric Lagergren Jul 3 '17 at 5:39

14 Answers 14

7
\$\begingroup\$

JavaScript (ES6), 69 bytes

d=>new Date(d).toLocaleString(0,{hour:n='numeric',minute:n,hour12:1})

f=
d=>new Date(d).toLocaleString(0,{hour:n='numeric',minute:n,hour12:1})
console.log(
  f('Fri Jun 30 2017 21:14:20 GMT-0700 (PDT)'),
  f('Fri Jun 30 2017 00:10:23 GMT-0700 (PDT)'),
  f('Fri Jun 30 2017 12:10:23 GMT-0700 (PDT)'),
  f('Sat Jun 31 2018 8:06:20 GMT-0700 (PDT)'),
  f('Fri Jul 01 2017 01:14:20 GMT-0700 (PDT)'),
  f('Sat Apr 10 2020 09:06:20 GMT-0700 (PDT)'),
)


JavaScript (ES6), 58 55 bytes

Assumes you are in the United States.

d=>new Date(d).toLocaleTimeString().replace(/:.. /,' ')

f=
d=>new Date(d).toLocaleTimeString().replace(/:.. /,' ')
console.log(
  f('Fri Jun 30 2017 21:14:20 GMT-0700 (PDT)'),
  f('Fri Jun 30 2017 00:10:23 GMT-0700 (PDT)'),
  f('Fri Jun 30 2017 12:10:23 GMT-0700 (PDT)'),
  f('Sat Jun 31 2018 8:06:20 GMT-0700 (PDT)'),
  f('Fri Jul 01 2017 01:14:20 GMT-0700 (PDT)'),
  f('Sat Apr 10 2020 09:06:20 GMT-0700 (PDT)')
)


JavaScript (ES6), 81 78 bytes

Answer before outputting a leading 0 in single-digit hours was made optional and test cases without leading 0s were added.

d=>([m,s]=d.slice(16).split`:`,`0${m%12||12}:${s} ${m<12?'A':'P'}M`.slice(-8))

f=
d=>([m,s]=d.slice(16).split`:`,`0${m%12||12}:${s} ${m<12?'A':'P'}M`.slice(-8))
console.log(
  f('Fri Jun 30 2017 21:14:20 GMT-0700 (PDT)'),
  f('Sat Jun 31 2018 08:06:20 GMT-0700 (PDT)'),
  f('Fri Jul 01 2017 01:14:20 GMT-0700 (PDT)'),
  f('Sat Apr 10 2020 09:06:20 GMT-0700 (PDT)')
)

\$\endgroup\$
  • 1
    \$\begingroup\$ I was thinking about changing ${m>12?'P':'A'}M to ${"AP"[m>12]}M, but it doesn't work without casting the index to an integer (like with |0), which makes it the same length. Nicely golfed. \$\endgroup\$ – kamoroso94 Jul 3 '17 at 4:01
  • \$\begingroup\$ You can omit ,21 in the slice to save 3 bytes. \$\endgroup\$ – Rick Hitchcock Jul 3 '17 at 5:24
  • \$\begingroup\$ Returns 12:10 AM for Fri Jun 30 2017 12:10:23 GMT-0700 (PDT). Should be PM. Changing m>12 to m>11 should fix it. \$\endgroup\$ – Rick Hitchcock Jul 3 '17 at 5:34
  • \$\begingroup\$ to save you 4 bytes: d=>(new Date(d).toLocaleString(0,{hour:'numeric',minute:'numeric',hour12:1})) \$\endgroup\$ – zoecarver Jul 3 '17 at 13:35
  • \$\begingroup\$ In fact, you could actually save 25 bytes with the following: d=>(new Date(d).toLocaleTimeString().replace(/:\d+/,'')) \$\endgroup\$ – zoecarver Jul 3 '17 at 13:52
6
\$\begingroup\$

Python 2, 66 bytes

lambda s:`int(s[15:18])%12`+s[18:21]+'  APMM'[int(s[15:18])>11::2]

Try it online!

\$\endgroup\$
  • \$\begingroup\$ This fails for years that have less than 4 digits (although I'm unsure whether it has to work with those). \$\endgroup\$ – notjagan Jul 3 '17 at 1:09
  • \$\begingroup\$ I think that is alright. \$\endgroup\$ – zoecarver Jul 3 '17 at 1:11
  • \$\begingroup\$ @notjagan it doesn't have to, it says so in the question. \$\endgroup\$ – Fedone Jul 4 '17 at 8:59
  • \$\begingroup\$ @totallyhuman Whoops, sorry. \$\endgroup\$ – Fedone Jul 5 '17 at 12:06
  • \$\begingroup\$ Can you change int(s[15:18])>11 to s[15:18]>"11"? \$\endgroup\$ – ETHproductions Jul 12 '17 at 21:50
5
\$\begingroup\$

sh + coreutils, 22 bytes

date +%I:%M\ %p -d"$1"

(If seconds are allowed, then date +%r -d"$1" suffices.)

\$\endgroup\$
  • \$\begingroup\$ impressively few bytes! also well done \$\endgroup\$ – zoecarver Jul 3 '17 at 1:10
4
\$\begingroup\$

JavaScript (ES6), 77 bytes

Assumes that the year has 4 digits.

s=>`${([,,,h,m]=s.match(/\d./g),x=h%12||12)>9?x:'0'+x}:${m} ${'AP'[h/12|0]}M`

Test cases

let f =

s=>`${([,,,h,m]=s.match(/\d./g),x=h%12||12)>9?x:'0'+x}:${m} ${'AP'[h/12|0]}M`

console.log(f('Fri Jun 30 2017 21:14:20 GMT-0700 (PDT)')) // 09:14 PM
console.log(f('Fri Jun 30 2017 00:10:23 GMT-0700 (PDT)')) // 12:10 AM
console.log(f('Fri Jun 30 2017 12:10:23 GMT-0700 (PDT)')) // 12:10 PM
console.log(f('Sat Jun 31 2018 08:06:20 GMT-0700 (PDT)')) // 08:06 AM
console.log(f('Fri Jul 01 2017 01:14:20 GMT-0700 (PDT)')) // 01:14 AM
console.log(f('Sat Apr 10 2020 09:06:20 GMT-0700 (PDT)')) // 09:06 AM

\$\endgroup\$
  • \$\begingroup\$ Like above, I have found a smaller way to create and answer. Feel free to use my code, I don't think that it is really fare to answer my own question. d=>(new Date(d).toLocaleTimeString().replace(/:\d+/,'')) \$\endgroup\$ – zoecarver Jul 4 '17 at 0:26
  • \$\begingroup\$ @pudility This would only work if your Locale is en-US and your timezone is GMT-0700 (PDT). For instance, none of these assumptions are true for me. \$\endgroup\$ – Arnauld Jul 4 '17 at 5:57
2
\$\begingroup\$

Japt, 15 bytes

ÐU¯24)s8 r.³+SS

Try it online!

12 bytes if we can assume that the time will be given in the computer's local time:

ÐU s8 r.³+SS

Try it online!

Mathy approach, 40 bytes

tG5 r"^.."_<C?+Z+B:°TnZ)%CÄÃ+" {"AP"gT}M

Test it online!

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

V, 36 bytes

16x3wC AMÇ^0ü^1[0-2]/12WrP
ç^ä:/é0

Try it online!

Hexdump:

00000000: 3136 7833 7743 2041 4d1b c75e 30fc 5e31  16x3wC AM..^0.^1
00000010: 5b30 2d32 5d2f 3132 1857 7250 0ae7 5ee4  [0-2]/12.WrP..^.
00000020: 3a2f e930                                :/.0
\$\endgroup\$
1
\$\begingroup\$

PHP, 45 bytes

Answer improved thanks to manatwork

<?=(new DateTime($argv[1]))->format('h:i A');

First attempt:

<? $d=new DateTime($argv[1]);echo$d->format('h:i A');

Example usage through php CLI:

php d.php "Sat Apr 10 2020 09:06:20 GMT-0700 (PDT)"

This is my first golf try.

\$\endgroup\$
  • \$\begingroup\$ No need for variable $d and that way you can get rid of the explicit echo: <?=(new DateTime($argv[1]))->format('h:i A');. \$\endgroup\$ – manatwork Jul 3 '17 at 9:43
1
\$\begingroup\$

Jelly, 43 bytes

Ḳ5ịṣ”:Ṗṁ3µV’%12‘Dµ1¦µV>11ị⁾PAµ3¦“0: M”żFṫ-7

Try it online!

This is superfluously too long! That is, Jelly sucks at time manipulation.

EDIT: I'm even outgolfed by PHP!

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

Go, 103 bytes

func f(s*string){t,_:=time.Parse("Mon Jan 02 2006 15:04:05 MST-0700 (MST)",*s)
*s=t.Format("03:04 PM")}

Test here: https://play.golang.org/p/P1zRWGske-

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

05AB1E, 39 bytes

#4è':¡¨`sD11›„APès<12%>0ìR2£R)Á… :M‚øJJ

Try it online!

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

PHP, 42 bytes

<?=date_create($argv[1])->format('h:i A');

Try it online!

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

C#, 145 bytes

namespace System.Linq{s=>{var d=DateTime.Parse(string.Join(" ",s.Split(' ').Skip(1).Take(4)));return d.ToString("h:mm ")+(d.Hour>11?"PM":"AM");}}

Full/Formatted version:

namespace System.Linq
{
    class P
    {
        static void Main()
        {
            Func<string, string> f = s =>
            {
                var d = DateTime.Parse(string.Join(" ", s.Split(' ').Skip(1).Take(4)));

                return d.ToString("h:mm ") + (d.Hour > 11 ? "PM" : "AM");
            };

            Console.WriteLine(f("Fri Jun 30 2017 21:14:20 GMT-0700 (PDT)"));
            Console.WriteLine(f("Fri Jun 30 2017 00:10:23 GMT-0700 (PDT)"));
            Console.WriteLine(f("Fri Jun 30 2017 12:10:23 GMT-0700 (PDT)"));
            Console.WriteLine(f("Fri Jul 01 2017 01:14:20 GMT-0700 (PDT)"));
            Console.WriteLine(f("Sat Apr 10 2020 09:06:20 GMT-0700 (PDT)"));

            Console.ReadLine();
        }
    }
}
\$\endgroup\$
0
\$\begingroup\$

,,,, 41 bytes

::18⊢3⊣⇆15⊢3⊣i11>"  APMM"⇆⊢2⟛↔15⊢3⊣i12%s#

Explanation

WIP

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

MATL, 9 bytes

5:24)16XO

Try it at MATL online! Or verify all test cases.

Explanation

5:24    % Push array [5 6 ... 24]
)       % Implicit input. Get characters at those positions. This
        % removes the first four characters with the day of the week
16      % Push 16
XO      % Convert to date string format 16, which is 'HH:MM PM'
        % Implicitly display
\$\endgroup\$
  • \$\begingroup\$ Can we have an explanation? \$\endgroup\$ – totallyhuman Jul 4 '17 at 21:19
  • 1
    \$\begingroup\$ @totallyhuman Sure, thanks for reminding me. Edited. Basically the builtin XO does most of the work \$\endgroup\$ – Luis Mendo Jul 4 '17 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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