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 2
3
0
\$\begingroup\$

AWK, 17 bytes

$0=strftime("%F")

Try it online!

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

jamal, 32 characters

{@format date=YEAR-0M-0D}{@date}

(Only interesting part is jamal's unusual date format specifiers. For example YYYY would mean YY twice, where YY is the year as in Perl, since 1900.)

Sample run:

bash-4.4$ jamal.pl date.jam 
2018-03-19
\$\endgroup\$
0
\$\begingroup\$

Gema, 40 characters

\A=@subst{*\\/*\\/*=\$3-\*-\*;@date}@end

Sample run:

bash-4.4$ gema '\A=@subst{*\\/*\\/*=\$3-\*-\*;@date}@end'
2018-03-19
\$\endgroup\$
0
\$\begingroup\$

Japt, 5 bytes

AîKs3

Try it

A         :10
 î        :Slice the following to that length
  K       :  Current date & time
   s3     :  Convert to ISO string
\$\endgroup\$
0
\$\begingroup\$

Forth (gforth), 52 bytes

: x 0 <# # # #> ." -"type ; : f time&date 1 .r x x ;

Try it online!

Code Explanation

\ x prints a dash followed by a 2-digit number with leading zeros
: x               \ start a new word definition
  0 <# # # #>     \ convert number to double-precision, then converts to a string with leading 0
  ." -"type       \ print "-" followed by the string from the previous command
;                 \ end the word definition
 
: f               \ start a new word definition
  time&date       \ forth built-in to get the components of a date/time (year on top)
  1 .r            \ print the year (with no space appended)
  x               \ print a dash, followed by the month
  x               \ print a dash, followed by the day
;                 \ end word definition
\$\endgroup\$
0
\$\begingroup\$

Nim, 32 bytes

import times
echo ($now())[0..9]

Try it online!

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

TI-Basic (TI-84), 32 bytes

Output(1,1,sum(getDate{ᴇ6,ᴇ3,1
Output(1,5,"-
Output(1,8,"-
  • getDate is a list in the form {2021, 1, 20}.
  • the date list is multiplied by {1e6, 1e3, 1} and then summed, giving a single number in the form yyyy0mm0dd, this deals automatically with leading zeros
  • -s are then added at the right places, giving the output yyyy-mm-dd
\$\endgroup\$
0
\$\begingroup\$

Thunno 2 h, 3 bytes

kQO

Attempt This Online!

Explanation

kQ   # Push a string of the current time
     # in the format "YYYY-MM-DD HH:mm:ss"
  O  # Split it on spaces to get the list
     # ["YYYY-MM-DD", "HH:mm:ss"]
     # Implicit output of first item
     # ("YYYY-MM-DD")
\$\endgroup\$
1 2
3

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.