29
\$\begingroup\$

Challenge

Weirdly, this hasn't been done yet: output the current date.

Rules

The date format you should follow is as follows:

YYYY-MM-DD

Where the month and day should be padded by zeroes if they are less than 10.

For example, if the program is run on the 24th May 2017, it should output

2017-05-24

The date can either be always in UTC or in the local date.

You must handle leaps years. i.e. in leap years, February has 29 days but 28 days in a normal year.

Winning

Shortest code in bytes wins.

\$\endgroup\$
9
  • 1
    \$\begingroup\$ ... and in normal years February has 31 days of course \$\endgroup\$
    – edc65
    Commented May 24, 2017 at 21:05
  • \$\begingroup\$ @edc65 I should really have known that Feb is 29 days long on leap years :P \$\endgroup\$
    – Beta Decay
    Commented May 24, 2017 at 21:21
  • \$\begingroup\$ Finally a golf where golfing languages cant perform \$\endgroup\$ Commented May 25, 2017 at 6:04
  • \$\begingroup\$ RIP Python needing the word datetime twice \$\endgroup\$ Commented May 25, 2017 at 8:38
  • \$\begingroup\$ In TIO, if I use p in Ruby it surrounds the output in quotes, but the actual date is in the right format. Is this okay? \$\endgroup\$
    – snail_
    Commented May 25, 2017 at 12:32

68 Answers 68

12
\$\begingroup\$

Bash with GNU coreutils, 16 7 bytes

-8 bytes thanks to Neil (and fergusq) (no pipe required to output)
-1 byte thanks to 12431234123412341234123 (use the built-in option with flag -I!)

date -I

Try it online!

\$\endgroup\$
8
  • 2
    \$\begingroup\$ Why the echo $(...)? \$\endgroup\$
    – Neil
    Commented May 24, 2017 at 20:28
  • \$\begingroup\$ Does it not need it to output? \$\endgroup\$ Commented May 24, 2017 at 20:29
  • 7
    \$\begingroup\$ date outputs already, the output is piped to stdout. You don't need to pipe it to echo, which pipes it to stdout. Try it online! \$\endgroup\$
    – fergusq
    Commented May 24, 2017 at 20:30
  • \$\begingroup\$ But even if you really want to use echo, then at least echo `date +%F`. \$\endgroup\$
    – manatwork
    Commented May 24, 2017 at 20:31
  • 6
    \$\begingroup\$ why not use -I ? \$\endgroup\$ Commented May 25, 2017 at 7:10
10
\$\begingroup\$

PHP, 17 bytes

<?=date('Y-m-d');
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Actually, YYYY-MM-DDThh:mm:ss+hh:mm is also an ISO 8601 formatted date and these 11 bytes: <?=date(c); print the whole stuff. ;) \$\endgroup\$
    – Titus
    Commented May 24, 2017 at 20:58
  • \$\begingroup\$ <?=strstr(date(c),T,1); to give the c format a little chance \$\endgroup\$ Commented May 24, 2017 at 21:51
7
\$\begingroup\$

Japt, 6 bytes

Ks3 ¯A

Try it online!

Explanation:

Ks3 ¯A
K       // New Date()
 s3     // .toISOString()
    ¯A  // .slice(0,10)
\$\endgroup\$
7
\$\begingroup\$

Bash, 15 bytes

printf '%(%F)T'

Sample run:

bash-4.4$ printf '%(%F)T'
2017-05-24

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Even better than I found on SO, was unaware of T. \$\endgroup\$ Commented May 24, 2017 at 20:24
  • \$\begingroup\$ @JonathanAllan, it was added only in Bash 4.2 and until Bash 4.3 it required an argument to format. \$\endgroup\$
    – manatwork
    Commented May 24, 2017 at 20:28
5
\$\begingroup\$

JavaScript (ES6), 34 bytes

_=>new Date().toJSON().split`T`[0]

f=

_=>new Date().toJSON().split`T`[0]

console.log(f());

\$\endgroup\$
1
  • \$\begingroup\$ Hmm, same length as _=>new Date().toJSON().slice(0,10) \$\endgroup\$ Commented May 25, 2017 at 7:51
4
\$\begingroup\$

SQLite, 13 characters

select date()

Good boy, SQLite. Other SQL dialects usually need either current_date or date(now()).

Sample run:

bash-4.4$ sqlite3 <<< 'select date()'
2017-05-24
\$\endgroup\$
1
  • 1
    \$\begingroup\$ MySQL has curdate \$\endgroup\$
    – Titus
    Commented May 28, 2017 at 22:06
4
\$\begingroup\$

Mathematica, 20 bytes

DateString@"ISODate"
\$\endgroup\$
1
  • 9
    \$\begingroup\$ Sometimes I come into these challenges not to see if Mathematica has a built-in, but to see what it is \$\endgroup\$
    – PunPun1000
    Commented May 25, 2017 at 18:03
4
\$\begingroup\$

Perl 6,  14  12 bytes

Date.today.say

Try it

now.Date.say

Try it

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

Excel, 24 bytes

=TEXT(NOW(),"yyy-mm-dd")

Excel will still do a 4-digit year with only 3 y's.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Google sheets will add the "). Excel won't. It will add the ) on the end but only after prompting you so it would require additional user input and drive the score up again. 24 bytes is probably the shortest Excel answer unless we allow for regional settings using the ISO date format by default and that's up for debate. \$\endgroup\$ Commented May 25, 2017 at 13:43
  • \$\begingroup\$ @EngineerToast Thanks. I'll fix that. \$\endgroup\$ Commented May 25, 2017 at 22:56
2
\$\begingroup\$

R, 10 bytes

Sys.Date()

Try it online!

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Since sys.date already is a function, you don't have to add the (), so this is only 8 bytes :) \$\endgroup\$
    – JAD
    Commented May 25, 2017 at 12:52
  • \$\begingroup\$ @JarkoDubbeldam not true, the () is necessary \$\endgroup\$
    – D. Nelson
    Commented May 26, 2017 at 12:34
  • \$\begingroup\$ @D.Nelson codegolf.meta.stackexchange.com/questions/2419/… Sys.Date is a function, so would suffice as solution. No need to explicitly call it. \$\endgroup\$
    – JAD
    Commented May 26, 2017 at 12:54
  • \$\begingroup\$ @JarkoDubbeldam that's normally the case but I think that since the challenge is to 'output the current date in ISO-8601 format' I actually need to output it. \$\endgroup\$
    – Giuseppe
    Commented May 26, 2017 at 13:38
  • 1
    \$\begingroup\$ Well yeah, and the last line of Sys.Date does the outputting for you. It is similar to when you have a solution that starts with function(x), you don't end that with an explicit call either. It's just how built-ins are scored. \$\endgroup\$
    – JAD
    Commented May 26, 2017 at 13:53
2
\$\begingroup\$

jq, 19 characters

(15 characters code + 4 characters command line options)

now|todate[:10]

Sample run:

bash-4.4$ jq -nr 'now|todate[:10]'
2017-05-24

Try in jq‣play

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

Lua, 18 characters

print(os.date"%F")

Sample run:

bash-4.4$ lua -e 'print(os.date"%F")'
2017-05-24

Try it online!

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

Alice, 9 bytes

/oT\
@%;'

Try it online!

Explanation

I'll leave the exact control flow as an exercise to the reader, but the linearised code that is being run in Ordinal mode is:

%T'T%;o@

And here is what it does:

%   Split an implicit empty string around an implicit empty string. Really doesn't
    do anything at all.
T   Push the current datetime as a string like "2017-05-24T20:53:08.150+00:00"
'T  Push "T".
%   Split the datetime string around the "T", to separate the date from the time.
;   Discard the time.
o   Output the date.
@   Terminate the program.

One way this might be golfable is to reuse the % to terminate the program in Cardinal mode with a division by zero, but the only layout I've come up with is the following:

\;T
\%o'

But here, the % doesn't actually terminate the program, because we push 111 ('o) right beforehand so there's never a division by zero.

In principle it might also be possible to reuse % to get rid of the ;, since trying to split the date around the time will simply discard the time.

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

SmileBASIC 3, 29 bytes

SB has a date string built in... but it's in the wrong format! It uses slashes instead of dashes, no good. Plus, being the self-respecting BASIC it is, there is no global replace function. I guess I have to do it myself...

D$=DATE$D$[4]="-
D$[7]="-
?D$
\$\endgroup\$
2
\$\begingroup\$

Java 8, 26 32 bytes

()->java.time.LocalDate.now()+""

Fixed format thanks to Kevin Cruijssen

\$\endgroup\$
2
  • \$\begingroup\$ Wow, Java is actually short... I'd never have thought I'd see the day :D \$\endgroup\$
    – Beta Decay
    Commented May 25, 2017 at 12:18
  • \$\begingroup\$ @BetaDecay It is short, but currently not complying to standard codegolf rules. It should be a function instead of a snippet, and the required imports should be counted as well. So it should be ()->java.time.LocalDate.now().toString() instead. Then again, you can golf .toString() to +"", so it becomes ()->java.time.LocalDate.now()+"" in total (32 bytes). \$\endgroup\$ Commented May 26, 2017 at 9:56
2
\$\begingroup\$

VBA, 5 25 bytes

?Date unpredictable, dependent on system short date settings

?Format(Now,"yyyy-mm-dd")

Output:

2017-05-25 
\$\endgroup\$
4
  • 7
    \$\begingroup\$ This answer is exceedingly volatile and by deafult, will not work on most computers as it depends entirely upon the user having their system short date format [under settings -> Date & Time in Win10] set to 'YYYY-MM-DD. The default format for this is M/D/YYYY` and thus this for the output of the Date function is 5/25/2017. To correct this your answer would need to be wrapped in a Format call. \$\endgroup\$ Commented May 25, 2017 at 13:44
  • 2
    \$\begingroup\$ @TaylorScott to that end ?format(now,"yyyy-mm-dd") using now is shorter than date \$\endgroup\$
    – Greedo
    Commented May 25, 2017 at 16:22
  • 1
    \$\begingroup\$ @Greedo, that is correct. This is, to my knowledge, the shortest way to achieve this goal using VBA \$\endgroup\$ Commented May 25, 2017 at 18:16
  • 1
    \$\begingroup\$ @TaylorScott thanks for pointing this out, I wasn't aware of the dependence on the system date settings. This date format is the default on my PC and in my country in general, so I was a little overconfident here. \$\endgroup\$ Commented May 26, 2017 at 8:45
2
\$\begingroup\$

Prolog (SWI), 46 bytes

f(X):-get_time(Y),format_time(atom(X),'%F',Y).

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Commented Jan 2, 2018 at 10:59
2
\$\begingroup\$

Pyth, 19 bytes

++++.d3"-".d4"-".d5

In Pyth . allows you to access a number of extra syntactic goodies. d contains various date options which are accessed with .d#, where # is a number 0-9. 3 is for the current year, 4 is the current month, and 5 is for the current day.

Format for the challenge wants dashes between each element of the date so we append them. Pyth using Polish notation for all math operands, thus the ++++.

\$\endgroup\$
2
  • \$\begingroup\$ Consider adding a short explanation of your code for those who don't know Pyth. Short code-only answers (like this one) tend to be automatically flagged as low quality. \$\endgroup\$
    – mbomb007
    Commented May 31, 2019 at 13:27
  • \$\begingroup\$ Thanks I'll do that! \$\endgroup\$ Commented May 31, 2019 at 15:40
2
\$\begingroup\$

Factor, 21 bytes

[ now timestamp>ymd ]

Try it online!

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

x86-16 machine code, IBM PC DOS, 55 53 48 bytes

Binary:

00000000: b42a cd21 91b3 64f6 f38b c8e8 1400 8ac5  .*.!..d.........
00000010: e80f 00b0 2dcd 298a c6e8 0600 b02d cd29  ....-.)......-.)
00000020: 8ac2 d40a 0530 3050 86c4 cd29 58cd 29c3  .....00P...)X.).

Listing:

B4 2A       MOV  AH, 2AH        ; get system date: CX=year, DH=month, DL=day 
CD 21       INT  21H            ; call DOS API
91          XCHG AX, CX         ; move year in CX to AX (dividend)
B3 64       MOV  BL, 100        ; divide by 100 to separate digits of year
F6 F3       DIV  BL             ; AL = first two digits, AH = last two digits
8B C8       MOV  CX, AX         ; save AX to CX to display last two digits later 
E8 0122     CALL DISP_WORD      ; display decimal value in AL 
8A C5       MOV  AL, CH         ; move last two digits to AL 
E8 0122     CALL DISP_WORD      ; display decimal value in AL 
B0 2D       MOV  AL, '-'        ; date separator 
CD 29       INT  29H            ; fast write AL to screen
8A C6       MOV  AL, DH         ; move month in DH into AL for display 
E8 0122     CALL DISP_WORD      ; display decimal value in AL 
B0 2D       MOV  AL, '-'        ; date separator 
CD 29       INT  29H            ; fast write AL to screen 
8A C2       MOV  AL, DL         ; move day into AL and display 
        DISP_WORD: 
D4 0A       AAM                 ; convert binary to BCD 
05 3030     ADD  AX, '00'       ; convert BCD to ASCII char 
50          PUSH AX             ; save second digit for display later 
86 C4       XCHG AL, AH         ; convert endian, AL = first digit, AH = second digit 
CD 29       INT  29H            ; fast write AL to screen
58          POP  AX             ; restore digits
CD 29       INT  29H            ; fast write AL to screen 
C3          RET                 ; return to caller/DOS

Output

Standalone DOS program, run from the command line outputs current date to the screen.

enter image description here

Much of this program is converting binary values to ASCII for console output. This uses (abuses) the binary-to-BCD instruction (AAM) to create a printable ASCII version of the output.

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

TI-Basic, 72 bytes

getDate→A
toString(Ans(1→B
toString(ʟA(2→C
toString(ʟA(3→D
If C<10
"0"+C→C
If D<10
"0"+D→D
B+"-"+C+"-"+D

This only works on a TI-84+ CE OS 5.2 and if the user has already set the time and date correctly.

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

QBIC, 33 bytes

B=_D?_sB,-4|+@-`+_sB,2|+A+_sB,4,2

Explanation:

B=_D        Assign the system's date to B$
            This is in American mm-dd-yyyy format, so we'll need to do some reformatting
?_sB,-4|    PRINT substring B, take 4 chars from the right
 +@-`         plus the string literal "-", now A$
 +_sB,2|      plus the leftmost two chars
 +A           and A$ again
 +_sB,4,2     plus the middle part.
\$\endgroup\$
1
\$\begingroup\$

CJam, 22 15 bytes

et3<{sY0e[}%'-*

Try it online!

-7 bytes thanks to Challenger5.

Explanation:

et                       Get array with [year,month,day,stuff...]
  3<                     Slice array to get [y,m,d] 
    {                    For each item do:
     s                     To string
      Y0e[                 add a 0 to the beginning of the string if it is shorter than 2 chars.
          }%             End for each
            '-*          Join the array with "-" as a separator
\$\endgroup\$
2
  • \$\begingroup\$ You can use e[ (pad array) for 15 bytes: et3<{sY0e[}%'-* \$\endgroup\$ Commented May 24, 2017 at 20:35
  • \$\begingroup\$ @Challenger5 that's cool. Thanks \$\endgroup\$
    – FrodCube
    Commented May 24, 2017 at 20:38
1
\$\begingroup\$

Python 2, 40 bytes

from datetime import*;print date.today()
\$\endgroup\$
0
1
\$\begingroup\$

Go, 62 56 bytes

import."time"
func f()string{return Now().String()[:10]}

Try it online!

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

Oracle SQL, 46 bytes

SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD') FROM DUAL
\$\endgroup\$
1
\$\begingroup\$

Powershell, 26 17 bytes

Date -f yyy-MM-dd

Thanks to @ConnorLSW for the 9 bytes.

\$\endgroup\$
3
  • \$\begingroup\$ you can save a lot here. date -f 'yyyy-MM-dd' \$\endgroup\$
    – colsw
    Commented May 25, 2017 at 10:10
  • \$\begingroup\$ Thanks Man. I am new to Powershell. So, I tried my luck here \$\endgroup\$ Commented May 25, 2017 at 10:13
  • \$\begingroup\$ @Shivaprasath V - no worries, check this thread for a couple of helpful posts with the more basic PS golfing tricks if you want. \$\endgroup\$
    – colsw
    Commented May 25, 2017 at 10:16
1
\$\begingroup\$

Ruby, 23 bytes

Prints the local time.

p Time.now.strftime'%F'
\$\endgroup\$
1
  • 1
    \$\begingroup\$ p Time.now.to_s[0..9] \$\endgroup\$
    – manatwork
    Commented May 25, 2017 at 14:33
1
\$\begingroup\$

MATLAB/Octave, 25 15 bytes

datestr(now,29)

Try it online!


The built-in function now returns the current system date in a weirdy MATLAB serial format.

datestr formats the weirdy serial format into a string of a requested format - which is in this case 'YYYY-mm-dd'. It turns out that MATLAB has a list of predefined formats for datestr. ISO8601 is one of them and is represented by the number 29, which allows a saving of 10 bytes.

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

Python 2,  53  40 bytes

-10 bytes thanks to Gábor Fekete (ISO-8601 is the default format for a date object)

from datetime import*
print date.today()

Try it online!

How?

datetime.date.today() will return a datetime.date object containing the local date information.

print will print a string representation of that object, this will call the object's __str__ function.

From the docs:

  • date.__str__(): For a date d, str(d) is equivalent to d.isoformat().

  • date.isoformat(): Return a string representing the date in ISO 8601 format, ‘YYYY-MM-DD’. For example, date(2002, 12, 4).isoformat() == '2002-12-04'.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ import datetime;print datetime.date.today() this is only 43 bytes and uses datetime. Why is the even shorter version deleted? \$\endgroup\$ Commented May 25, 2017 at 12:05
  • \$\begingroup\$ @GáborFekete Thanks (I think you should really have posted, it's sufficiently different). The other answer was probably deleted because it did not print the correctly formatted string. I have added an explanation about why this does actually work (so long as one includes the print). Saved another three doing (the evil) import*. \$\endgroup\$ Commented May 25, 2017 at 17:42
  • \$\begingroup\$ I wanted to but there was an another solution which was even shorter than mine but was deleted for some reason. \$\endgroup\$ Commented May 26, 2017 at 8:39
  • \$\begingroup\$ ...which has now been edited to include the print while deleted then undeleted. \$\endgroup\$ Commented May 26, 2017 at 8:43

Your Answer

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

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