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 PM12:10 AM12:10 PM08:06 AM1:14 AM09: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 AM09:06 AM
All tested years will be after
999(each year will be exactly4digits)
Rules
- This is code-golf so the shortest solution in bytes wins
- Standard Loopholes Apply
- You may use functions as well as programs
15 Answers 15
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)')
)
-
1\$\begingroup\$ I was thinking about changing
${m>12?'P':'A'}Mto${"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– kamoroso942017年07月03日 04:01:38 +00:00Commented Jul 3, 2017 at 4:01 -
\$\begingroup\$ You can omit
,21in the slice to save 3 bytes. \$\endgroup\$Rick Hitchcock– Rick Hitchcock2017年07月03日 05:24:39 +00:00Commented 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. Changingm>12tom>11should fix it. \$\endgroup\$Rick Hitchcock– Rick Hitchcock2017年07月03日 05:34:18 +00:00Commented 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\$zoecarver– zoecarver2017年07月03日 13:35:00 +00:00Commented 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\$zoecarver– zoecarver2017年07月03日 13:52:28 +00:00Commented Jul 3, 2017 at 13:52
-
\$\begingroup\$ This fails for years that have less than 4 digits (although I'm unsure whether it has to work with those). \$\endgroup\$notjagan– notjagan2017年07月03日 01:09:20 +00:00Commented Jul 3, 2017 at 1:09
-
\$\begingroup\$ I think that is alright. \$\endgroup\$zoecarver– zoecarver2017年07月03日 01:11:09 +00:00Commented Jul 3, 2017 at 1:11
-
\$\begingroup\$ @notjagan it doesn't have to, it says so in the question. \$\endgroup\$Fedone– Fedone2017年07月04日 08:59:28 +00:00Commented Jul 4, 2017 at 8:59
-
\$\begingroup\$ @totallyhuman Whoops, sorry. \$\endgroup\$Fedone– Fedone2017年07月05日 12:06:29 +00:00Commented Jul 5, 2017 at 12:06
-
\$\begingroup\$ Can you change
int(s[15:18])>11tos[15:18]>"11"? \$\endgroup\$ETHproductions– ETHproductions2017年07月12日 21:50:55 +00:00Commented Jul 12, 2017 at 21:50
sh + coreutils, 22 bytes
date +%I:%M\ %p -d"1ドル"
(If seconds are allowed, then date +%r -d"1ドル" suffices.)
-
\$\begingroup\$ impressively few bytes! also well done \$\endgroup\$zoecarver– zoecarver2017年07月03日 01:10:29 +00:00Commented Jul 3, 2017 at 1:10
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
-
\$\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– zoecarver2017年07月04日 00:26:14 +00:00Commented Jul 4, 2017 at 0:26 -
\$\begingroup\$ @pudility This would only work if your Locale is
en-USand your timezone isGMT-0700 (PDT). For instance, none of these assumptions are true for me. \$\endgroup\$Arnauld– Arnauld2017年07月04日 05:57:02 +00:00Commented Jul 4, 2017 at 5:57
Japt, 15 bytes
ÐU ̄24)s8 r.3+SS
12 bytes if we can assume that the time will be given in the computer's local time:
ÐU s8 r.3+SS
Mathy approach, 40 bytes
tG5 r"^.."_<C?+Z+B:°TnZ)%CÄÃ+" {"AP"gT}M
V, 36 bytes
16x3wC AMÇ^0ü^1[0-2]/12WrP
ç^ä:/é0
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
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.
-
\$\begingroup\$ No need for variable
$dand that way you can get rid of the explicitecho:<?=(new DateTime($argv[1]))->format('h:i A');. \$\endgroup\$manatwork– manatwork2017年07月03日 09:43:05 +00:00Commented Jul 3, 2017 at 9:43
Jelly, 43 bytes
Ḳ5ịṣ":Ṗṁ3μV’%12‘Dμ1¦μV>11ị)PAμ3¦"0: M"żFṫ-7
This is superfluously too long! That is, Jelly sucks at time manipulation.
EDIT: I'm even outgolfed by PHP!
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-
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();
}
}
}
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
-
\$\begingroup\$ Can we have an explanation? \$\endgroup\$totallyhuman– totallyhuman2017年07月04日 21:19:19 +00:00Commented Jul 4, 2017 at 21:19
-
1\$\begingroup\$ @totallyhuman Sure, thanks for reminding me. Edited. Basically the builtin
XOdoes most of the work \$\endgroup\$Luis Mendo– Luis Mendo2017年07月04日 21:28:17 +00:00Commented Jul 4, 2017 at 21:28
AWK, 66 bytes
split(5,ドルa,":")&&0ドル=((b=a[1]%12)?b:12)":"a[2](a[1]>11?" PM":" AM")
00:10:23 --> 12:10 AMand12:10:23 --> 12:10 PM. \$\endgroup\$'Fri Jun 30 2017 21:14:20 GMT-0400 (EDT)'for me) \$\endgroup\$