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

asked Jul 3, 2017 at 0:55
\$\endgroup\$
10
  • \$\begingroup\$ Suggested test cases: 00:10:23 --> 12:10 AM and 12:10:23 --> 12:10 PM. \$\endgroup\$ Commented Jul 3, 2017 at 1:23
  • \$\begingroup\$ there we go. Feel free to edit if you see anything else I should change. thanks for the suggestion! \$\endgroup\$ Commented Jul 3, 2017 at 1:25
  • \$\begingroup\$ Related \$\endgroup\$ Commented Jul 3, 2017 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\$ Commented Jul 3, 2017 at 1:37
  • 9
    \$\begingroup\$ erm, June 31 doesn't exist. Is that accurate? \$\endgroup\$ Commented Jul 3, 2017 at 5:39

15 Answers 15

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

answered Jul 3, 2017 at 1:19
\$\endgroup\$
6
  • 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\$ Commented Jul 3, 2017 at 4:01
  • \$\begingroup\$ You can omit ,21 in the slice to save 3 bytes. \$\endgroup\$ Commented Jul 3, 2017 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\$ Commented Jul 3, 2017 at 5:34
  • \$\begingroup\$ to save you 4 bytes: d=>(new Date(d).toLocaleString(0,{hour:'numeric',minute:'numeric',hour12:1})) \$\endgroup\$ Commented Jul 3, 2017 at 13:35
  • \$\begingroup\$ In fact, you could actually save 25 bytes with the following: d=>(new Date(d).toLocaleTimeString().replace(/:\d+/,'')) \$\endgroup\$ Commented Jul 3, 2017 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!

answered Jul 3, 2017 at 1:06
\$\endgroup\$
6
  • \$\begingroup\$ This fails for years that have less than 4 digits (although I'm unsure whether it has to work with those). \$\endgroup\$ Commented Jul 3, 2017 at 1:09
  • \$\begingroup\$ I think that is alright. \$\endgroup\$ Commented Jul 3, 2017 at 1:11
  • \$\begingroup\$ @notjagan it doesn't have to, it says so in the question. \$\endgroup\$ Commented Jul 4, 2017 at 8:59
  • \$\begingroup\$ @totallyhuman Whoops, sorry. \$\endgroup\$ Commented Jul 5, 2017 at 12:06
  • \$\begingroup\$ Can you change int(s[15:18])>11 to s[15:18]>"11"? \$\endgroup\$ Commented Jul 12, 2017 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.)

answered Jul 3, 2017 at 1:02
\$\endgroup\$
1
  • \$\begingroup\$ impressively few bytes! also well done \$\endgroup\$ Commented Jul 3, 2017 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

answered Jul 3, 2017 at 1:29
\$\endgroup\$
2
  • \$\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\$ Commented Jul 4, 2017 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\$ Commented Jul 4, 2017 at 5:57
2
\$\begingroup\$

Japt, 15 bytes

ÐU ̄24)s8 r.3+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.3+SS

Try it online!

Mathy approach, 40 bytes

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

Test it online!

answered Jul 3, 2017 at 1:34
\$\endgroup\$
0
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
answered Jul 3, 2017 at 2:17
\$\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.

answered Jul 3, 2017 at 9:39
\$\endgroup\$
1
  • \$\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\$ Commented Jul 3, 2017 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!

answered Jul 3, 2017 at 11:38
\$\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-

answered Jul 3, 2017 at 5:46
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 39 bytes

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

Try it online!

answered Jul 3, 2017 at 12:00
\$\endgroup\$
0
\$\begingroup\$

PHP, 42 bytes

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

Try it online!

answered Jul 3, 2017 at 13:41
\$\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();
 }
 }
}
answered Jul 4, 2017 at 9:09
\$\endgroup\$
0
\$\begingroup\$

,,,, 41 bytes

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

Explanation

WIP

answered Jul 4, 2017 at 18:07
\$\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
Suever
11.2k1 gold badge24 silver badges52 bronze badges
answered Jul 3, 2017 at 8:55
\$\endgroup\$
2
  • \$\begingroup\$ Can we have an explanation? \$\endgroup\$ Commented Jul 4, 2017 at 21:19
  • 1
    \$\begingroup\$ @totallyhuman Sure, thanks for reminding me. Edited. Basically the builtin XO does most of the work \$\endgroup\$ Commented Jul 4, 2017 at 21:28
0
\$\begingroup\$

AWK, 66 bytes

split(5,ドルa,":")&&0ドル=((b=a[1]%12)?b:12)":"a[2](a[1]>11?" PM":" AM")

Attempt This Online!

answered Sep 30 at 15:18
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.