| Bytes | Lang | Time | Link |
|---|---|---|---|
| 066 | AWK | 250930T151850Z | xrs |
| 009 | MATL | 170703T085500Z | Luis Men |
| 041 | 170704T180744Z | totallyh | |
| 069 | JavaScript ES6 | 170703T011958Z | darrylye |
| 145 | C# | 170704T090928Z | TheLetha |
| 043 | Jelly | 170703T113810Z | Erik the |
| 015 | Japt | 170703T013448Z | ETHprodu |
| 042 | PHP | 170703T134148Z | kip |
| 039 | 05AB1E | 170703T120054Z | Erik the |
| 045 | PHP | 170703T093906Z | jankes |
| 103 | Go | 170703T054632Z | Eric Lag |
| 036 | V | 170703T021759Z | DJMcMayh |
| 077 | JavaScript ES6 | 170703T012941Z | Arnauld |
| 022 | sh + coreutils | 170703T010223Z | Anders K |
| 066 | Python 2 | 170703T010616Z | totallyh |
AWK, 66 bytes
split($5,a,":")&&$0=((b=a[1]%12)?b:12)":"a[2](a[1]>11?" PM":" AM")
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
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)')
)
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();
}
}
}
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!
Japt, 15 bytes
ÐU¯24)s8 r.³+SS
12 bytes if we can assume that the time will be given in the computer's local time:
ÐU s8 r.³+SS
Mathy approach, 40 bytes
tG5 r"^.."_<C?+Z+B:°TnZ)%CÄÃ+" {"AP"gT}M
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.
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-
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
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
sh + coreutils, 22 bytes
date +%I:%M\ %p -d"$1"
(If seconds are allowed, then date +%r -d"$1" suffices.)