8
\$\begingroup\$

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
\$\endgroup\$
  • 2
    \$\begingroup\$ 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? \$\endgroup\$ – AdmBorkBork Jan 11 at 19:43
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – skiilaa Jan 11 at 19:50
  • 1
    \$\begingroup\$ Does the timezone matter? \$\endgroup\$ – Erik the Outgolfer Jan 11 at 21:52
  • \$\begingroup\$ I take it the year is represented by a two digit number? \$\endgroup\$ – Embodiment of Ignorance Jan 11 at 22:22
  • 1
    \$\begingroup\$ @FabianRöling timezones don't matter. \$\endgroup\$ – skiilaa Jan 12 at 18:04

21 Answers 21

3
\$\begingroup\$

Japt v1.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

Notes / Tips

\$\endgroup\$
8
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ Wow. Just, wow. \$\endgroup\$ – skiilaa Jan 13 at 9:08
  • 1
    \$\begingroup\$ 64 bytes: n=>'235689.11121415'.replace(/1?\w/g,x=>new Date(n).toJSON()[x]) \$\endgroup\$ – tsh Jan 14 at 7:02
  • \$\begingroup\$ @tsh Nice! I was not expecting the decimal format to be shorter. You may want to post this as a separate answer. \$\endgroup\$ – Arnauld Jan 14 at 15:45
7
\$\begingroup\$

Bash + coreutils, 29 bytes

date -d@${1::-3} +%y%m%d.%H%M

Try it online!

\$\endgroup\$
6
\$\begingroup\$

PHP, 40 32 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.

\$\endgroup\$
  • \$\begingroup\$ Nice answer! It's a tad shorter if the language has date formatting, yes :) \$\endgroup\$ – skiilaa Jan 11 at 20:03
  • \$\begingroup\$ 32 bytes you dont need to cast to integer since "2"/1 will cast automatically the string \$\endgroup\$ – Luis felipe De jesus Munoz Jan 11 at 20:04
  • \$\begingroup\$ ah of course, I added intval before realising I needed /1000, and didn't think that I mihgt not need it afterwards :P \$\endgroup\$ – Skidsdev Jan 11 at 20:08
  • \$\begingroup\$ @JoKing ah nice one, thanks \$\endgroup\$ – Skidsdev Jan 14 at 14:03
5
\$\begingroup\$

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

\$\endgroup\$
4
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ Thank you, @JoKing. Unfortunately E-notation is not really my friend so I always forget it. \$\endgroup\$ – manatwork Jan 13 at 16:02
4
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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/- //}
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – Neil A. Jan 11 at 22:43
  • 2
    \$\begingroup\$ @NeilA. Yes. The import can go either before or after, the contents of the lambda aren't checked for NameErrors before it's called. \$\endgroup\$ – Erik the Outgolfer Jan 11 at 22:45
3
\$\begingroup\$

R, 58 56 bytes

format(as.POSIXct(scan()/1e3,,'1970-1-1'),'%y%m%d.%H%M')

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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));}
\$\endgroup\$
  • \$\begingroup\$ What's the extra s argument you're taking for? \$\endgroup\$ – Shaggy Jan 12 at 14:47
  • 1
    \$\begingroup\$ @Shaggy The s is for the output string. \$\endgroup\$ – nwellnhof Jan 12 at 15:06
  • \$\begingroup\$ It looks like you're initiating it outside the function; do we have a meta consensus that allows C to do that? \$\endgroup\$ – Shaggy Jan 12 at 15:31
  • \$\begingroup\$ @Shaggy I'm not sure what "consensus" means exactly but here's the relevant meta post. \$\endgroup\$ – nwellnhof Jan 12 at 15:54
  • \$\begingroup\$ With "32-bit,", why long long instead of long or int32_t, int64_t, time_t? IAC, all shorter than long long. \$\endgroup\$ – chux Jan 13 at 22:23
2
\$\begingroup\$

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.

Explanation:

                       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 '-'
\$\endgroup\$
2
\$\begingroup\$

C# (Visual C# Interactive Compiler), 67 61 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!

\$\endgroup\$
  • 1
    \$\begingroup\$ It seems UnixEpoch is only present in.Net Core 2.1+ \$\endgroup\$ – digEmAll Jan 12 at 12:19
2
\$\begingroup\$

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

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!

\$\endgroup\$
  • \$\begingroup\$ gmtime is short than localtime \$\endgroup\$ – chux Jan 13 at 22:08
2
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

JavaScript, 64 bytes

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

Try it online!

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

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!

\$\endgroup\$
  • 3
    \$\begingroup\$ You don't need to count command-line flags anymore. \$\endgroup\$ – AdmBorkBork Jan 11 at 20:28
  • \$\begingroup\$ Oops. Good to know. Thank you @AdmBorkBork. \$\endgroup\$ – manatwork Jan 11 at 20:36
  • \$\begingroup\$ 1000 -> 1e3 \$\endgroup\$ – Shaggy Jan 14 at 20:16
1
\$\begingroup\$

ksh, 36 bytes

printf "%(%y%m%d.%H%M)T" $(($1/1e3))

Try it online!

Thanks to Jo King for 15 bytes saved

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

MediaWiki, 46 bytes

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

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
\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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