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
    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
    May 24, 2017 at 21:21
  • \$\begingroup\$ Finally a golf where golfing languages cant perform \$\endgroup\$ May 25, 2017 at 6:04
  • \$\begingroup\$ RIP Python needing the word datetime twice \$\endgroup\$ 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_
    May 25, 2017 at 12:32

68 Answers 68

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
  • 2
    \$\begingroup\$ I'm pretty sure you can use %F. Also, nice to see Crystal on PPCG! \$\endgroup\$ May 24, 2017 at 20:38
  • 1
    \$\begingroup\$ @Nick Clifford I saw it on tio and thought it had a cool name. \$\endgroup\$ May 24, 2017 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\$
5
  • 2
    \$\begingroup\$ Where do I download Sytem? \$\endgroup\$ May 25, 2017 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\$ May 25, 2017 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\$ May 25, 2017 at 13:13
  • 1
    \$\begingroup\$ @EriktheOutgolfer Damn, next time I'll create a shorter wrapper as well... \$\endgroup\$ May 25, 2017 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, 2017 at 10:37
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\$
1
  • \$\begingroup\$ Can't you remove a few of those spaces? \$\endgroup\$
    – Timtech
    Jun 4, 2017 at 19:11
1
\$\begingroup\$

Noether, 8 bytes

6D0 10SP

Try it here!

Explanation:

6  - Push the number 6 onto the stack
D  - Pop the mode number off the top of the stack and push the ISO date string (Mode 6)
0  - Push the number 0 onto the stack
10 - Push the number 10 onto the stack
S  - Pop the three items, A, B and C off the top of the stack and slice the string, A, from B to C
P  - Print the item on the top of the stack

Alternatively, you could use the following method (also for 8 bytes):

6D"T"^#P

Try it here!

Explanation:

6   - Push 6 onto the stack
D   - Pop the mode number off the top of the stack and push the ISO date string (Mode 6)
"T" - Push the string "T" onto the stack
^   - Pop two strings A and B off the stack and split A by B
#   - Pop the top item off the stack
P   - Print the item on the top of the stack
\$\endgroup\$
1
\$\begingroup\$

Swift, 13 bytes

print(Date())
\$\endgroup\$
1
\$\begingroup\$

q/kdb+, 18 16 bytes

Solution:

ssr[.z.d$:].".-"

Example:

q)ssr[.z.d$:].".-"
"2018-01-01"

Explanation:

Take the string representation of today's date, and replace the dots with dashes.

ssr[string .z.d;".";"-"] / fully ungolfed solution
ssr[string .z.d;;].".-"  / partially ungolfed solution
           .z.d          / current UTC date (2018.01.01)
    string               / convert to string ("2018.01.01")
ssr[           ;;]       / string-search-replace 
                  .".-"  / the two parameters
\$\endgroup\$
1
\$\begingroup\$

J, 16 bytes

6!:0'YYYY-MM-DD'

Try it online!

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

vim, 18

i®=strftime("%F")↵

where ® and are Ctrl-R and Enter.

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

Zsh, 15 bytes

Who needs date?

print -P %D{%F}

Try it online!

If we assume the program will only be run in current century, we can save 2 bytes:

print -P 20%D
\$\endgroup\$
1
  • \$\begingroup\$ "If we assume the program will only be run in current century" — This is precisely how Y2K happened… \$\endgroup\$
    – xigoi
    Sep 6, 2021 at 18:59
1
\$\begingroup\$

05AB1E, 16 13 bytes

Saved 3 bytes thanks to Kevin Cruijssen

žgžfže)T‰J'-ý

Try it online!

Explanation

žgžfže)         # push [year, month, day]
       T‰       # divmod each by 10
         J      # join each div and mod result together
          '-ý   # join year-month-day on "-"
\$\endgroup\$
3
  • \$\begingroup\$ It's a shame that žgžfže‚т+€¦`'-ý doesn't work. \$\endgroup\$ May 25, 2017 at 12:45
  • \$\begingroup\$ 13 bytes \$\endgroup\$ Mar 25, 2019 at 14:20
  • 1
    \$\begingroup\$ @KevinCruijssen: The divmod trick being useful again. I'm definitely going to have to remember that ;) \$\endgroup\$
    – Emigna
    Mar 25, 2019 at 15:04
1
\$\begingroup\$

Perl 6, 12 bytes

say now.Date

Try it online!

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

Vyxal, 2 bytes

kD

Try it Online!

Uh, it's a built in constant using python's date.today().isoformat().

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

Jelly, 45 bytes

“from datetime import*;print(date.today())”ŒV

Try it online!

evaluates python code because comedy

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

Fourier, 33 bytes

|~D<10{1}{0o}Do|F5do`-`4d^F`-`3dF

Try it online!

Uses a function which adds the leading zero... So long :P

\$\endgroup\$
0
\$\begingroup\$

Python 2, 59 bytes

Oddly enough, I can't find an easier built-in... somebody else found it. ><

from datetime import*
print datetime.now().isoformat()[:10]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

SAS, 31 bytes

%put %sysfunc(date(),yymmdd10.);
\$\endgroup\$
1
  • \$\begingroup\$ Use e8601da. to save one byte. \$\endgroup\$
    – whymath
    Dec 6, 2022 at 3:42
0
\$\begingroup\$

MATL, 6 bytes

Z'29XO

Try it online!

Explanation

Z'    % Push current date and time as a float
29    % Push 29
XO    % String representation of date and time with specified format.
      % Format 29 corresponds to 'yyyy-mm-dd'. Implicitly display
\$\endgroup\$
0
\$\begingroup\$

C#, 51 bytes

Console.Write(DateTime.Now.ToString("yyyy-MM-dd"));
\$\endgroup\$
4
  • 2
    \$\begingroup\$ You can save 7 bytes by using string interpolation - Console.Write($"{DateTime.Now:yyyy-MM-dd}"); \$\endgroup\$
    – Chris
    May 25, 2017 at 7:28
  • \$\begingroup\$ This is just a code snippet, you need to wrap it in a function/program. You also need to fully qualify the classes or add the appropriate usings. \$\endgroup\$ May 25, 2017 at 10:40
  • 1
    \$\begingroup\$ Or look at @TheLethalCoder example on making a function that returns it rather than writing to standard output, most questions allow this. \$\endgroup\$
    – LiefdeWen
    May 25, 2017 at 13:04
  • \$\begingroup\$ @Chris Thanks! I didn't know that. \$\endgroup\$
    – Micah Epps
    May 25, 2017 at 14:01
0
\$\begingroup\$

Pyth, 20 bytes

j\-+.d3m.[\02`.d+4d2

Try it here.

\$\endgroup\$
0
\$\begingroup\$

C, 104 79 61 bytes

Saved 18 bytes thanks to hvd.

n,b[99];f(){time(&n);strftime(b,99,"%F",gmtime(&n));puts(b);}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ "strftime segfaults if you don't #include <time.h>." -- Not on all implementations. You could get rid of that line and save some bytes by simply choosing an implementation where it works. (Most x86-32 Linux distros probably work.) \$\endgroup\$
    – hvd
    May 25, 2017 at 8:36
0
\$\begingroup\$

VBA, 26 Bytes

Anonymous VBE Immediate window function that outputs the current date in the specified format, regardless of system settings, to the VBE immediate window

?Format(Now,"YYYY-MM-DD")
\$\endgroup\$
0
\$\begingroup\$

PowerShell, 16 bytes

date -u %Y-%m-%d

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Haskell, 55 bytes

import Data.Time
m=getCurrentTime>>=putStr.take 10.show

Not exactly the shortest answer (even the Java answer is shorter!)

Explanation

import Data.Time                            --put the function getCurrentTime into scope
m=getCurrentTime>>=putStr.take 10.show
m=                                          --define a variable/function m
  getCurrentTime                            --get the current time wrapped in the IO type
                >>=                         --feed the current time into the following function
                                  show      --display the time as a string
                          take 10.          --take the first ten characters of that to get the date
                   putStr.                  --print it
\$\endgroup\$
0
\$\begingroup\$

Objective-C, 27 bytes

NSLog(@"%@",[NSDate date]);
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Even shorter: NSLog(@"%@",[NSDate new]); \$\endgroup\$
    – JAL
    Aug 8, 2017 at 20:54
0
\$\begingroup\$

Python, 39 bytes

Beats this answer by 1 byte.

from datetime import*;str(date.today())

This won't actually work when put in a file and executed, but it does work when you paste this in a python shell. However, it works if you have this in a file and then call that file's __main__ function, so I don't know if this is allowed by the rules. I'm going to assume that it is.

\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog), 17 bytes

'-'@5 8⍕1E3⊥3↑⎕TS

Try it online!

⎕TS Y M D h m s ms

3↑ take the first three

1E3⊥ evaluate in base-1000

 make into string

'-'@5 8 substitute a dash at positions five and eight

\$\endgroup\$
0
\$\begingroup\$

Windows batch, 166 144 136 107 bytes

@set X=
@for /f "skip=1" %%x in ('wmic os get localdatetime')do @set X=%%x
@echo %X:~0,4%-%X:~4,2%-%X:~6,2%

Outputs local time

\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 86 chars

(multiple-value-bind(a b c d e f)(get-decoded-time)(format t"~4a-~2,'0d-~2,'0d"f e d))

Good old verbose Common Lisp!

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Funky, 18 bytes

@os.date"%Y-%m-%d"

Try it online!

\$\endgroup\$

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.