25
\$\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\$
  • 14
    \$\begingroup\$ Happy 10k rep ! \$\endgroup\$ – Rohan Jhunjhunwala May 24 '17 at 20:42
  • 1
    \$\begingroup\$ And congrats on the fastest growing thread I´ve ever seen. :D \$\endgroup\$ – Titus May 24 '17 at 20:59
  • \$\begingroup\$ @Titus You should have seen Hello, World! :D \$\endgroup\$ – Beta Decay May 24 '17 at 21:00
  • \$\begingroup\$ ... and in normal years February has 31 days of course \$\endgroup\$ – edc65 May 24 '17 at 21:05
  • \$\begingroup\$ @edc65 I should really have known that Feb is 29 days long on leap years :P \$\endgroup\$ – Beta Decay May 24 '17 at 21:21

60 Answers 60

12
\$\begingroup\$

Bash, 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\$
  • 2
    \$\begingroup\$ Why the echo $(...)? \$\endgroup\$ – Neil May 24 '17 at 20:28
  • \$\begingroup\$ Does it not need it to output? \$\endgroup\$ – Jonathan Allan May 24 '17 at 20:29
  • 6
    \$\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 May 24 '17 at 20:30
  • \$\begingroup\$ But even if you really want to use echo, then at least echo `date +%F`. \$\endgroup\$ – manatwork May 24 '17 at 20:31
  • 5
    \$\begingroup\$ why not use -I ? \$\endgroup\$ – 12431234123412341234123 May 25 '17 at 7:10
9
\$\begingroup\$

PHP, 17 bytes

<?=date('Y-m-d');
\$\endgroup\$
  • 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 May 24 '17 at 20:58
  • \$\begingroup\$ <?=strstr(date(c),T,1); to give the c format a little chance \$\endgroup\$ – Jörg Hülsermann May 24 '17 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\$
  • \$\begingroup\$ Even better than I found on SO, was unaware of T. \$\endgroup\$ – Jonathan Allan May 24 '17 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 May 24 '17 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\$
  • \$\begingroup\$ Hmm, same length as _=>new Date().toJSON().slice(0,10) \$\endgroup\$ – Steve Bennett May 25 '17 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
    \$\begingroup\$ MySQL has curdate \$\endgroup\$ – Titus May 28 '17 at 22:06
4
\$\begingroup\$

Mathematica, 20 bytes

DateString@"ISODate"
\$\endgroup\$
  • 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 May 25 '17 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
    \$\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\$ – Engineer Toast May 25 '17 at 13:43
  • \$\begingroup\$ @EngineerToast Thanks. I'll fix that. \$\endgroup\$ – Scott Milner May 25 '17 at 22:56
2
\$\begingroup\$

R, 10 bytes

Sys.Date()

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Since sys.date already is a function, you don't have to add the (), so this is only 8 bytes :) \$\endgroup\$ – JAD May 25 '17 at 12:52
  • \$\begingroup\$ @JarkoDubbeldam not true, the () is necessary \$\endgroup\$ – D. Nelson May 26 '17 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 May 26 '17 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 May 26 '17 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 May 26 '17 at 13:53
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\$

VBA, 5 25 bytes

?Date unpredictable, dependent on system short date settings

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

Output:

2017-05-25 
\$\endgroup\$
  • 6
    \$\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\$ – Taylor Scott May 25 '17 at 13:44
  • 2
    \$\begingroup\$ @TaylorScott to that end ?format(now,"yyyy-mm-dd") using now is shorter than date \$\endgroup\$ – Greedo May 25 '17 at 16:22
  • 1
    \$\begingroup\$ @Greedo, that is correct. This is, to my knowledge, the shortest way to achieve this goal using VBA \$\endgroup\$ – Taylor Scott May 25 '17 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\$ – Maciej Lipinski May 26 '17 at 8:45
2
\$\begingroup\$

Prolog (SWI), 46 bytes

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

Try it online!

\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ – Martin Ender Jan 2 '18 at 10:59
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\$
  • \$\begingroup\$ You can use e[ (pad array) for 15 bytes: et3<{sY0e[}%'-* \$\endgroup\$ – Esolanging Fruit May 24 '17 at 20:35
  • \$\begingroup\$ @Challenger5 that's cool. Thanks \$\endgroup\$ – FrodCube May 24 '17 at 20:38
1
\$\begingroup\$

Python 2, 40 bytes

from datetime import*;print date.today()
\$\endgroup\$
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\$
  • \$\begingroup\$ you can save a lot here. date -f 'yyyy-MM-dd' \$\endgroup\$ – colsw May 25 '17 at 10:10
  • \$\begingroup\$ Thanks Man. I am new to Powershell. So, I tried my luck here \$\endgroup\$ – Sivaprasath Vadivel May 25 '17 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 May 25 '17 at 10:16
1
\$\begingroup\$

Ruby, 23 bytes

Prints the local time.

p Time.now.strftime'%F'
\$\endgroup\$
  • 1
    \$\begingroup\$ p Time.now.to_s[0..9] \$\endgroup\$ – manatwork May 25 '17 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\$
  • 1
    \$\begingroup\$ import datetime;print datetime.date.today() this is only 43 bytes and uses datetime. Why is the even shorter version deleted? \$\endgroup\$ – Gábor Fekete May 25 '17 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\$ – Jonathan Allan May 25 '17 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\$ – Gábor Fekete May 26 '17 at 8:39
  • \$\begingroup\$ ...which has now been edited to include the print while deleted then undeleted. \$\endgroup\$ – Jonathan Allan May 26 '17 at 8:43
1
\$\begingroup\$

Crystal, 30 bytes 24 bytes 21 bytes

-6 thanks to Nick Clifford

-3 from looking at snail_'s answer in Ruby

p Time.now.to_s("%F")

Try it online!

\$\endgroup\$
  • 2
    \$\begingroup\$ I'm pretty sure you can use %F. Also, nice to see Crystal on PPCG! \$\endgroup\$ – Nick Clifford May 24 '17 at 20:38
  • 1
    \$\begingroup\$ @Nick Clifford I saw it on tio and thought it had a cool name. \$\endgroup\$ – TitusLucretius May 24 '17 at 20:44
1
\$\begingroup\$

Rust, 84 Bytes

extern crate chrono;fn main(){print!("{}",chrono::Local::now().format("%Y-%m-%d"));}
\$\endgroup\$
1
\$\begingroup\$

C#, 46 45 bytes

_=>System.DateTime.Now.ToString("yyy-MM-dd");

Saved a byte thanks to @raznagul.

\$\endgroup\$
  • 1
    \$\begingroup\$ Where do I download Sytem? \$\endgroup\$ – Erik the Outgolfer May 25 '17 at 13:09
  • 6
    \$\begingroup\$ @EriktheOutgolfer It's a custom wrapper I created around the System namespace because I kept doing the same typo... \$\endgroup\$ – TheLethalCoder May 25 '17 at 13:12
  • 4
    \$\begingroup\$ You know stuff you create locally isn't allowed on PPCG unless it's published before the question is asked. :P \$\endgroup\$ – Erik the Outgolfer May 25 '17 at 13:13
  • 1
    \$\begingroup\$ @EriktheOutgolfer Damn, next time I'll create a shorter wrapper as well... \$\endgroup\$ – TheLethalCoder May 25 '17 at 13:14
  • 1
    \$\begingroup\$ As leading zeroes for the year are not required the format string "yyy-MM-dd" will also work. \$\endgroup\$ – raznagul May 26 '17 at 10:37
1
\$\begingroup\$

Java 8, 26 32 bytes

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

Fixed format thanks to Kevin Cruijssen

\$\endgroup\$
  • \$\begingroup\$ Wow, Java is actually short... I'd never have thought I'd see the day :D \$\endgroup\$ – Beta Decay May 25 '17 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\$ – Kevin Cruijssen May 26 '17 at 9:56
1
\$\begingroup\$

C++14, 143 bytes 139 bytes

#include <cstdio>
#include <time.h>
int main (){time_t t;char D[11];time(&t);strftime(D,sizeof D,"%Y-%m-%d",localtime(&t));printf("%s",D);}

Timtech pointed out to me that i don't need all spaces

Test me!

\$\endgroup\$
  • \$\begingroup\$ Can't you remove a few of those spaces? \$\endgroup\$ – Timtech Jun 4 '17 at 19:11
1
\$\begingroup\$

Swift, 13 bytes

print(Date())
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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