What's the Date?

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.

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

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!

• Why the echo $(...)? – Neil May 24 '17 at 20:28 • Does it not need it to output? – Jonathan Allan May 24 '17 at 20:29 • 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! – fergusq May 24 '17 at 20:30 • But even if you really want to use echo, then at least echo date +%F. – manatwork May 24 '17 at 20:31 • why not use -I ? – 12431234123412341234123 May 25 '17 at 7:10 PHP, 17 bytes <?=date('Y-m-d');  • 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. ;) – Titus May 24 '17 at 20:58 • <?=strstr(date(c),T,1); to give the c format a little chance – Jörg Hülsermann May 24 '17 at 21:51 Japt, 6 bytes Ks3 ¯A  Try it online! Explanation: Ks3 ¯A K // New Date() s3 // .toISOString() ¯A // .slice(0,10)  Bash, 15 bytes printf '%(%F)T'  Sample run: bash-4.4$ printf '%(%F)T'
2017-05-24


Try it online!

• Even better than I found on SO, was unaware of T. – Jonathan Allan May 24 '17 at 20:24
• @JonathanAllan, it was added only in Bash 4.2 and until Bash 4.3 it required an argument to format. – manatwork May 24 '17 at 20:28

JavaScript (ES6), 34 bytes

_=>new Date().toJSON().splitT[0]


f=

_=>new Date().toJSON().splitT[0]

console.log(f());

• Hmm, same length as _=>new Date().toJSON().slice(0,10) – Steve Bennett May 25 '17 at 7:51

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  Mathematica, 20 bytes DateString@"ISODate"  • Sometimes I come into these challenges not to see if Mathematica has a built-in, but to see what it is – PunPun1000 May 25 '17 at 18:03 Perl 6, 14 12 bytes Date.today.say  Try it now.Date.say  Try it Excel, 24 bytes =TEXT(NOW(),"yyy-mm-dd")  Excel will still do a 4-digit year with only 3 y's. • 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. – Engineer Toast May 25 '17 at 13:43 • @EngineerToast Thanks. I'll fix that. – Scott Milner May 25 '17 at 22:56 R, 10 bytes Sys.Date()  Try it online! • Since sys.date already is a function, you don't have to add the (), so this is only 8 bytes :) – JAD May 25 '17 at 12:52 • @JarkoDubbeldam not true, the () is necessary – D. Nelson May 26 '17 at 12:34 • @D.Nelson codegolf.meta.stackexchange.com/questions/2419/… Sys.Date is a function, so would suffice as solution. No need to explicitly call it. – JAD May 26 '17 at 12:54 • @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. – Giuseppe May 26 '17 at 13:38 • 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. – JAD May 26 '17 at 13:53 Lua, 18 characters print(os.date"%F")  Sample run: bash-4.4$ lua -e 'print(os.date"%F")'
2017-05-24


Try it online!

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

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$ VBA, 5 25 bytes ?Date unpredictable, dependent on system short date settings ?Format(Now,"yyyy-mm-dd")  Output: 2017-05-25  • 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. – Taylor Scott May 25 '17 at 13:44 • @TaylorScott to that end ?format(now,"yyyy-mm-dd") using now is shorter than date – Greedo May 25 '17 at 16:22 • @Greedo, that is correct. This is, to my knowledge, the shortest way to achieve this goal using VBA – Taylor Scott May 25 '17 at 18:16 • @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. – Maciej Lipinski May 26 '17 at 8:45 Prolog (SWI), 46 bytes f(X):-get_time(Y),format_time(atom(X),'%F',Y).  Try it online! • Welcome to PPCG! – Martin Ender Jan 2 '18 at 10:59 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.


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

• You can use e[ (pad array) for 15 bytes: et3<{sY0e[}%'-* – Esolanging Fruit May 24 '17 at 20:35
• @Challenger5 that's cool. Thanks – FrodCube May 24 '17 at 20:38

Python 2, 40 bytes

from datetime import*;print date.today()


Go, 62 56 bytes

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


Try it online!

Oracle SQL, 46 bytes

SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD') FROM DUAL


Powershell, 26 17 bytes

Date -f yyy-MM-dd


Thanks to @ConnorLSW for the 9 bytes.

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

Ruby, 23 bytes

Prints the local time.

p Time.now.strftime'%F'

• p Time.now.to_s[0..9] – manatwork May 25 '17 at 14:33

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.

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

• import datetime;print datetime.date.today() this is only 43 bytes and uses datetime. Why is the even shorter version deleted? – Gábor Fekete May 25 '17 at 12:05
• @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*. – Jonathan Allan May 25 '17 at 17:42
• I wanted to but there was an another solution which was even shorter than mine but was deleted for some reason. – Gábor Fekete May 26 '17 at 8:39
• ...which has now been edited to include the print while deleted then undeleted. – Jonathan Allan May 26 '17 at 8:43

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!

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

Rust, 84 Bytes

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


C#, 46 45 bytes

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


Saved a byte thanks to @raznagul.

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

Java 8, 26 32 bytes

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


Fixed format thanks to Kevin Cruijssen

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

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!

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

Swift, 13 bytes

print(Date())
`