Unix timestamp to datetime string

Given a unix timestamp as an input, give a datetime string, in a format like so: "YYMMDD.HHmm"

Rules

• The input is a number (integer) of a millisecond-precise UNIX epoch time (milliseconds since 1970 January 1st 00:00:00.000 UTC).
• The values must be padded with zeroes if they are 1 character instead of 2. (e.g.: for "DD", "1" is not acceptable, but "01" is.)
• The output must be a single string. No arrays.
• Leap second handling doesn't matter.
• Shortest wins.

Good luck!

Example

Input: 1547233866744
Output: 190111.1911

• Welcome to PPCG! Nice challenge, but there are a few clarifications to be made. To make the challenge self-contained, you should probably explain what the Unix epoch is. Additionally, what should be done with leap seconds? What should be done with the Year 2038 problem? – AdmBorkBork Jan 11 at 19:43
• @AdmBorkBork Edited the post to explain what the Unix epoch is. Leap second implementation doesn't matter, since the output string is not second-precise. The Year 2038 problem doesn't currently matter since it could be a limitation of the running device or the chosen programming language. – skiilaa Jan 11 at 19:50
• Does the timezone matter? – Erik the Outgolfer Jan 11 at 21:52
• I take it the year is represented by a two digit number? – Embodiment of Ignorance Jan 11 at 22:22
• @FabianRöling timezones don't matter. – skiilaa Jan 12 at 18:04

Japtv1.4.5, 19 16 bytes

GîÐU s3)¤o>J i.G


1 byte saved thanks to Oliver, which led to another 2 bytes saved.

Try it

Explanation

GîÐU s3)¤o>J i.G
:Implicit input of integer U
G                    :16
î                   :Get the first 16 characters of the following string
ÐU                 :  Convert U to a date object
s3              :  Convert to ISO string
)             :End get
¤            :Slice off the first 2 characters
o           :Filter
>J         :  Greater than -1
i.G     :Insert "." at 0-based index 16, with wrapping


JavaScript (ES6), 65 bytes

n=>'101010.1010'.replace(i=/\d/g,x=>new Date(n).toJSON()[i=x-~i])


Try it online!

How?

We initialize the pointer $$\i\$$ to a non-numeric value (coerced to $$\0\$$) and then add alternately $$\2\$$ and $$\1\$$ to it to pick the relevant characters from the ISO-8601 conversion of the input timestamp.

yyyy-mm-ddThh:mm:ss.sssZ
^^ ^^ ^^ ^^ ^^


JavaScript (ES6), 66 bytes

n=>'235689.BCEF'.replace(/\w/g,x=>new Date(n).toJSON()[+0x${x}])  Try it online! How? Once the input timestamp is converted in ISO-8601 format, all required characters can be accessed with a single hexadecimal digit. yyyy-mm-ddThh:mm:ss.sssZ ↓↓ ↓↓ ↓↓ ↓↓ ↓↓ 0123456789ABCDEF  • Wow. Just, wow. – skiilaa Jan 13 at 9:08 • 64 bytes: n=>'235689.11121415'.replace(/1?\w/g,x=>new Date(n).toJSON()[x]) – tsh Jan 14 at 7:02 • @tsh Nice! I was not expecting the decimal format to be shorter. You may want to post this as a separate answer. – Arnauld Jan 14 at 15:45 Bash + coreutils, 29 bytes date -d@${1::-3} +%y%m%d.%H%M


Try it online!

PHP, 4032 31 bytes

-8 bytes thanks to Luis felipe
-1 byte thanks to Jo King

<?=date('ymd.hi',$argv[1]/1e3);  Try it online! Simple naive answer. PHP's date function takes a format string and an integer timestamp. Input from cli arguments, which is a string by default, then /1e3 because date expects second-precise timestamps. This also coerces the string to a number. • Nice answer! It's a tad shorter if the language has date formatting, yes :) – skiilaa Jan 11 at 20:03 • 32 bytes you dont need to cast to integer since "2"/1 will cast automatically the string – Luis felipe De jesus Munoz Jan 11 at 20:04 • ah of course, I added intval before realising I needed /1000, and didn't think that I mihgt not need it afterwards :P – Skidsdev Jan 11 at 20:08 • @JoKing ah nice one, thanks – Skidsdev Jan 14 at 14:03 MATL, 28 bytes Thanks to @skiilaa for a correction in the output format. 864e5/719529+'YYmmDD.HHMM'XO  Try it online! Explanation MATL, like MATLAB, defines date/time numbers as the (possibly non-integer) number of days since time 00:00 of the reference "date" 0-Jan-0000. Thus we take the input, divide it by 86400000 (number of milliseconds in one day), add 719529 (number of days from MATL's reference to UNIX reference), and convert to the desired format 'YYmmDD.HHMM'. PowerShell, 59 58 bytes "{0:yyMMdd.HHmm}"-f(Date 1/1/1970).AddSeconds("$args"/1e3)


Try it online!

Gets the Date of 1/1/1970 (defaults to 00:00:00am), then Adds the appropriate number of Seconds. Passes that to the -format operator, which correctly formats the datetime.

Probably culture-dependent. This works on TIO, which is en-us.

-1 byte thanks to shaggy.

GNU AWK, 34 33 characters

$0=strftime("%y%m%d.%H%M",$0/1e3)


(strftime() is GNU extension, will not run in other AWK implementations.)

Thanks to:

Sampler run:

bash-4.4$awk '$0=strftime("%y%m%d.%H%M",$0/1e3)' <<< 1547233866744 190111.2111  Try it online! • Thank you, @JoKing. Unfortunately E-notation is not really my friend so I always forget it. – manatwork Jan 13 at 16:02 Perl 6, 111 89 87 bytes {~DateTime.new($_/Ⅿ,:formatter{"{(.year%Ⅽ,.month,.day).fmt('%02d','')}.{(.hour,.minute).fmt('%02d','')}"})}


Try it (111)

{TR/-//}o{S/..//}o{.yyyy-mm-dd~'.'~(.hour,.minute).fmt('%02d','')}o{DateTime.new($_/Ⅿ)}  Try it (89) {TR/- //}o{S/..//}o{.yyyy-mm-dd~'.'~(.hour,.minute).fmt('%02d')}o{DateTime.new($_/Ⅿ)}


Try it (87)

Explanation:

The o infix operator takes two functions and creates a composite function. The rightmost one gets called first, and the one to the left gets called with its result.

Basically we use 4 block lambdas to generate a single lambda.

Which is not much different to how a WhateverCode lambda like * + * gets created.

Divide by 1000 and use that to create a DateTime object.

{DateTime.new($_/Ⅿ)} # Ⅿ is ROMAN NUMERAL ONE THOUSAND (3 bytes)  The result gets used by: { .yyyy-mm-dd # 2019-01-11 ~ '.' ~ # str concatenation with '.' ( .hour, .minute ).fmt('%02d') # add leading 0s (returns List) }  That leaves us with a string like 2019-01-11.19 11 We need to remove the first two digits {S/..//}  We also need to remove - and {TR/- //}  Python 2, 64 bytes lambda s:strftime('%y%m%d.%H%M',gmtime(s/1e3)) from time import*  Try it online! The input is considered to be in UTC. • Just as something I've been a bit confused on, is the only reason that the lambda comes before the import because it works better for TIO's header? – Neil A. Jan 11 at 22:43 • @NeilA. Yes. The import can go either before or after, the contents of the lambda aren't checked for NameErrors before it's called. – Erik the Outgolfer Jan 11 at 22:45 R, 58 56 bytes format(as.POSIXct(scan()/1e3,,'1970-1-1'),'%y%m%d.%H%M')  Try it online! C (gcc) (32-bit, little endian), 67 bytes f(t,s)long long t;{t/=1e3;strftime(s,12,"%y%m%d.%H%M",gmtime(&t));}  Try it online! On an ILP64 platform, the following 55 byte version should work: f(t,s){t/=1e3;strftime(s,12,"%y%m%d.%H%M",gmtime(&t));}  • What's the extra s argument you're taking for? – Shaggy Jan 12 at 14:47 • @Shaggy The s is for the output string. – nwellnhof Jan 12 at 15:06 • It looks like you're initiating it outside the function; do we have a meta consensus that allows C to do that? – Shaggy Jan 12 at 15:31 • @Shaggy I'm not sure what "consensus" means exactly but here's the relevant meta post. – nwellnhof Jan 12 at 15:54 • With "32-bit,", why long long instead of long or int32_t, int64_t, time_t? IAC, all shorter than long long. – chux - Reinstate Monica Jan 13 at 22:23 Perl 6, 57 50 bytes {TR:d/T:-/./}o{substr ~DateTime.new($_/1e3): 2,15}


Try it online!

Takes the default stringification of a Datetime, in the format yyyy-mm-ddThh:mm:ssZ and modifies it to fit the output format. Perl 6 is in need of a date formatter method.

                       Datetime.new($_/1e3) # Create a date time ~ # Stringify it to the format yyyy-mm-ddThh:mm:ssZ # e.g. 2019-01-11T19:11:06.744000Z substr : 2,15 # Take the middle 15 characters {TR:d/T /./}o # Then replace 'T' with '.' :- # Then remove ':' and '-'  C# (Visual C# Interactive Compiler), 6761 60 bytes n=>$"{new DateTime(1970,1,1).AddTicks(n*10000):yyMMdd.HHmm}"


For reasons unknown to me, DateTime.UnixEpoch doesn't work.

Try it online!

• It seems UnixEpoch is only present in.Net Core 2.1+ – digEmAll Jan 12 at 12:19

Javascript ES6, 76 66 bytes

x=>new Date(x).toJSON().slice(2,16).replace(/\D/g,a=>a>'S'?'.':'')


Try it online

-10 bytes thanks to Shaggy!

x // timestamp
=>
new Date(x) // date object from timestamp
.toJSON() // same as .toISOString()
.slice(2,16) // cut off excess
.replace(/\D/g, // match all non-digits
a // a is matched character
=>
a>'S'?'.' // if a is T (bigger than S is shorter) replace it with .
:''       // if it's not T, replace it with nothing
// this way the dashes get removed and the dot gets put in the right place
) // end of replace

• You may want to wait a day or so before answering your own questions next time. – fəˈnɛtɪk Jan 11 at 20:11
• 71 bytes – Luis felipe De jesus Munoz Jan 11 at 20:16
• Alternative 71 bytes – Shaggy Jan 11 at 20:33
• @LuisfelipeDejesusMunoz, that's different enough for you to post yourself. – Shaggy Jan 11 at 21:49
• @LuisfelipeDejesusMunoz 66 bytes to tie you with Arnauld. – Shaggy Jan 12 at 13:50

C (clang), 117 111 bytes

Thanks to @chux and @ceilingcat for the suggestions.

#import<time.h>
*l;f(long t){t/=1e3;printf("%02d%02d%02d.%02d%02d",5[l=gmtime(&t)]%100,l[4]+1,l[3],l[2],l[1]);}


Try it online!

• gmtime is short than localtime – chux - Reinstate Monica Jan 13 at 22:08

Twig, 25 characters

{{d[:-3]|date('ymd.hi')}}


This is a template. Call it by including it and pass the Unix time as parameter d.

Sample usage:

{{include('datetime.twig', {'d': 1547233866744})}}


Try it on TwigFiddle

JavaScript, 64 bytes

n=>'2356891911121415'.replace(/1?./g,x=>new Date(n).toJSON()[x])


Try it online!

jq, 33 characters

(30 characters code + 3 characters command line option)

./1000|strftime("%y%m%d.%H%M")


Sample run:

bash-4.4$jq -r './1000|strftime("%y%m%d.%H%M")' <<< 1547233866744 190111.1911  Try it online! • You don't need to count command-line flags anymore. – AdmBorkBork Jan 11 at 20:28 • Oops. Good to know. Thank you @AdmBorkBork. – manatwork Jan 11 at 20:36 • 1000 -> 1e3 – Shaggy Jan 14 at 20:16 ksh, 36 bytes printf "%(%y%m%d.%H%M)T"$((\$1/1e3))


Try it online!

Thanks to Jo King for 15 bytes saved

• The same in Bash would be just 35 characters: Try it online! – manatwork Jan 13 at 15:25

MediaWiki, 46 bytes

{{#time:ymd.Hi|@{{#expr:floor({{{1}}}/1e3)}}}}


Java 8, 78 bytes

n->new java.text.SimpleDateFormat("yyMMdd.HHmm").format(new java.util.Date(n))


Try it online.

Explanation:

n->                       // Method with long parameter and String return-type
new java.text.SimpleDateFormat("yyMMdd.HHmm")
//  Create the formatter
.format(               //  Format the date to a String in this format and return it:
new java.util.Date(  //   Create a new Date
n))                 //   With the input-long as timestamp